佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 2645|回复: 26

JAVA求助,大家帮帮忙

[复制链接]
发表于 3-10-2013 09:41 PM | 显示全部楼层 |阅读模式
2.     The interface for aclass named Aircond is given below.  
Nameof class: Aircond
Method:
switchOn()
Description:
Ifthe aircond is switched on, the following string will be displayed:
"The aircond is already on!"
Otherwise,the following string is displayed:
"The aircond is on."
Method:
switchOff()
Description:
Ifthe aircond is not switched on, the following string will be displayed:
"The aircond is already off!"
Otherwise,the following string is displayed:
"The aircond is off."
a.     Write a program in steps given below whichmakes use of the above class.
i.     Write a class named AircondTest which contains just theempty main method.  
ii.    Add statement(s) to create three Aircond objects.
iii.   Add statement(s) to send a switchOn message to the first and second Aircond objects.
iv.   Add statement(s) to send a switchOff message to the third Aircond object.
v.    Add statement(s) to send a switchOn message again to the first andsecond Aircond objects.
vi.   Add statement(s) to send a switchOff message to the first and third Aircond objects.
b.    Write the output of the program in (a).
3.      The interface for a class named Calculator is described below. For each of the following, write a program which inputs therequired values and uses a Calculatorobject to calculate the given expression. The program then outputs theresult. [10m]
  
Method
  
Return Type
Description
add(float x, float y)
float
Calculates x+y and returns the result.
minus(float x, float y)
float
Calculates x-y and returns the result.
times(float x, float y)
float
Calculates x*y and returns the result.
divide(float x, float y)
float
Calculates x/y (real division) and returns the  result.
memsto(float x)
void
Stores x into internal memory
memclr()
void
Resets  internal memory (to zero)
memget()
float
Returns value  in internal memory
memadd(float x)
void
Adds x to value in internal memory and  stores the result into internal memory.
sqroot(float x)
float
Calculates  square root of x and  returns the result.
power(float x, float y)
float
Calculates xy and returns the result.
a.             Inputs:              floatx, y, z
Expression:      file:///C:/Users/lenovo/AppData/Local/Temp/msohtmlclip1/01/clip_image002.gif
b.    Inputs:              float k, g, m, x
Expression:      x+(((m-(k-g))+x)-g)
c.              Inputs:              int t, x, y
                                    float k, g
Expression:      file:///C:/Users/lenovo/AppData/Local/Temp/msohtmlclip1/01/clip_image004.gif
d.             Inputs:              intx, z, m, n
Expression:      file:///C:/Users/lenovo/AppData/Local/Temp/msohtmlclip1/01/clip_image006.gif
4.     Based on the Calculator class in the previous question, determine what the followingprograms calculate.
a.              
calc.memclr();
     calc.memsto(calc.add(x, y));
     calc.memadd(calc.times(z, y));
     System.out.println(calc.memget());
b.
            calc.memclr();
     calc.memsto(calc.add(calc.times(2,x), 1));
     calc.memsto(calc.power(calc.add(y,z), calc.memget()));
     calc.memadd(calc.memget());
     System.out.println(calc.memget());


这个是我上课老师给的题目,其实我不是很明白program要我们写出什么...可能英文不太好的关系...有哪位大大可以帮忙解释一下关于问题要的是什么,或者给一下example,不求答案...我只求明白了在自己做而已...谢谢各位大大...
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 3-10-2013 10:33 PM | 显示全部楼层
SotongJiang 发表于 3-10-2013 10:30 PM
这两题最好先用纸笔,一面想,一面画出来,酱子解题的思路就会很清晰。

题目2,最关键的是class Aircond ...

哦,好的。谢谢你的回复..其实我也是拿笔出来写。可是弄不懂object之类的东西...我需要create多一个class(就是新的tab)来放那三个object吗?
回复

使用道具 举报

 楼主| 发表于 4-10-2013 08:04 AM | 显示全部楼层
SotongJiang 发表于 4-10-2013 12:32 AM
是的,那个是2(a)(i)的class AircondTest, 之后讲的Add statement(s) to都是写在AirconTest的main()里面, ...

谢谢你的帮忙..我做了整晚还是做不到
回复

使用道具 举报

 楼主| 发表于 4-10-2013 08:48 AM | 显示全部楼层
SotongJiang 发表于 4-10-2013 12:32 AM
是的,那个是2(a)(i)的class AircondTest, 之后讲的Add statement(s) to都是写在AirconTest的main()里面, ...

现在我已经在Aircond的class做了两个method,我要怎样在a1.switchOn()那边,当它call switchOn method时让program来判断aircond is on和aircond is already on..
我现在method里面放如果是a1.switchOn()就是aircond is on,而a2.switchOn()等于aircond already on.你会吗?请指导小弟一点..
回复

使用道具 举报

发表于 4-10-2013 09:55 AM | 显示全部楼层
shern91 发表于 4-10-2013 08:48 AM
现在我已经在Aircond的class做了两个method,我要怎样在a1.switchOn()那边,当它call switchOn method时让 ...

如果给我的话, 我会在aircond的class里加一个variable来代表那个aircond是不是开着.
  1. public class aircond{
  2.         ...
  3.         boolean isOn = off;
  4.         ...
  5. }
复制代码
回复

使用道具 举报

发表于 4-10-2013 10:59 AM 来自手机 | 显示全部楼层
shern91 发表于 4-10-2013 08:48 AM
现在我已经在Aircond的class做了两个method,我要怎样在a1.switchOn()那边,当它call switchOn method时让 ...

我又來了...

要program自己判斷,最簡單就是用if else。

不想害你,我給一點點code罷了...
  1. declare:
  2. int current = 0;
  3. static int isOn = 1;
  4. static int isOff = 0;

  5. public void switchOn() {
  6.    if (current == isOn) {
  7.       System.out.println("Aircond already on.");
  8.    } else {
  9.       current = isOn;
  10.       System.out.println("Aircond is on.");
  11.    }
复制代码
我沒有debug過的,不知道work沒有,你自己看著辦...
回复

使用道具 举报

Follow Us
 楼主| 发表于 4-10-2013 12:31 PM | 显示全部楼层
Jason929 发表于 4-10-2013 10:59 AM
我又來了...

要program自己判斷,最簡單就是用if else。

that variable static isOn = 1 got error...if declare in main that 2 method can take that variable...now at school sorry cant taip Chinese...
回复

使用道具 举报

 楼主| 发表于 4-10-2013 12:32 PM | 显示全部楼层
Urikon 发表于 4-10-2013 09:55 AM
如果给我的话, 我会在aircond的class里加一个variable来代表那个aircond是不是开着.

boolean isOn = 1;
nt have to use true or false de ma?
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 4-10-2013 01:01 PM | 显示全部楼层
see my coding,but no work..

//import java.util.Scanner;
public class Aircond{

        public static void main(String[] args) {
               
                int current = 0;
                //static int isOn = 1;
                //static int isOff = 0;
               
               
        }
               
                public void switchOn() {
                       
                       
                        boolean isOn=true;
                        if(isOn==true)
                        {
                        System.out.println("The aircond is on");
                        //isOn=false;
                        }
                        else if(isOn=false)
                        {
                        System.out.println("The aircond is already on");
                        }
                        isOn=false;
                       
                }
                public void switchOff() {
                       
                        int current = 0;
                        int isOff=1;
                       
                        if(current==isOff)
                        {
                        System.out.println("The aircond is off");
                        }
                        else
                        {
                        System.out.println("The aircond is already off");
                        }
                }
}
               


public class AircondTest {

        public static void main(String[] args) {
               
                
                        Aircond a1, a2, a3;

                a1 = new Aircond();
                a2 = new Aircond();
                a3 = new Aircond();

                a1.switchOn();  
                a2.switchOn();
                
                //a3.switchOff();
                
                //a1.switchOn();
                //a2.switchOn();
                
                //a1.switchOff();
                //a3.switchOff();
                
            }

}

where it wrong?
回复

使用道具 举报

 楼主| 发表于 4-10-2013 09:45 PM | 显示全部楼层
SotongJiang 发表于 4-10-2013 08:04 PM
不错,有个好的开始。

首先你的Java file可整理成类似这里的CubeStaticTest.java

你真的很用心...谢谢指导...我做成功了的话会贴出来分享...虽然不是什么厉害的程序...不过如果成功的话真的还蛮高兴的...
回复

使用道具 举报

 楼主| 发表于 4-10-2013 11:42 PM | 显示全部楼层
SotongJiang 发表于 4-10-2013 08:04 PM
不错,有个好的开始。

首先你的Java file可整理成类似这里的CubeStaticTest.java

public class Aircond {

        public static void main(String[] args) {
               
        }
                Aircond ab;
                //boolean isOn = true;
                static int isOn=0;
                //boolean isOff;//= false;
       
                public void switchOn() {
                       
                        if(isOn==0)
                        {       
                        System.out.println("The aircond is on");
                       
                        }
                        else
                        {
                        System.out.println("The aircond is already on");
                        }
                        ab.changeOn();
                }
                public void changeOn()
                {
                        isOn=1;//
                }
                public void switchOff() {
                       
                        if(isOn==1)
                        {
                        System.out.println("The aircond is off");
                        ab.changeOn();
                        }
                        else
                        {
                        System.out.println("The aircond is already off");
                        }
                }
               
                public void changeOff()
                {
                        isOn=0;//
                }
               
}
到最后,我做成这样...可是得到java.lang.NullPointerException error...我也试过用boolean,可是status就是不会转...哈哈...

回复

使用道具 举报

发表于 5-10-2013 12:33 PM | 显示全部楼层
我今天有空,帮你解决你的问题,不过我还是建议什么是class variable, method variable etc 和它们之间的分别。programming要学会必须多动手,单单会理论也不行。

Aircond

  1. public class Aircond
  2. {
  3.     private boolean isOn = false;

  4.     public void switchOn()
  5.     {
  6.         if( isOn )
  7.         {
  8.             System.out.println( "The aircond is already on!" );
  9.         }
  10.         else
  11.         {
  12.             isOn = true;
  13.             System.out.println( "The aircond is on." );
  14.         }
  15.     }

  16.     public void switchOff()
  17.     {
  18.         if( !isOn )
  19.         {
  20.             System.out.println( "The aircond is already off!" );
  21.         }
  22.         else
  23.         {
  24.             System.out.println( "The aircond is off." );
  25.             isOn = false;
  26.         }
  27.     }
  28. }
复制代码
AircondTest


  1. public class AircondTest
  2. {
  3.     public static void main( String[] args )
  4.     {
  5.         //Add statement(s) to create three Aircond objects.
  6.         Aircond aircond1 = new Aircond();
  7.         Aircond aircond2 = new Aircond();
  8.         Aircond aircond3 = new Aircond();

  9.         //Add statement(s) to send a switchOn message to the first and second Aircond objects.
  10.         aircond1.switchOn();
  11.         aircond2.switchOn();

  12.         //Add statement(s) to send a switchOff message to the third Aircond object.
  13.         aircond3.switchOff();

  14.         //Add statement(s) to send a switchOn message again to the first andsecond Aircond objects.
  15.         aircond1.switchOn();
  16.         aircond2.switchOn();

  17.         //Add statement(s) to send a switchOff message to the first and third Aircond objects.
  18.         aircond1.switchOff();
  19.         aircond3.switchOff();
  20.     }
  21. }
复制代码
Output Result

  1. The aircond is on.
  2. The aircond is on.
  3. The aircond is already off!
  4. The aircond is already on!
  5. The aircond is already on!
  6. The aircond is off.
  7. The aircond is already off!
复制代码
本帖最后由 黑木头 于 5-10-2013 12:40 PM 编辑

回复

使用道具 举报

 楼主| 发表于 5-10-2013 12:46 PM | 显示全部楼层
黑木头 发表于 5-10-2013 12:33 PM
我今天有空,帮你解决你的问题,不过我还是建议什么是class variable, method variable etc 和它们之间的分 ...

谢谢你哦...问你几个问
aircond1.switchOn();
aircond2.switchOn();

output不是应该像以下那样的吗?还是因为a1.aircond()开了,a2.aircond()还没开?if( !isOn ) <-的意思是isOn等于true?
aircond is on
aircond is already on
回复

使用道具 举报

发表于 5-10-2013 12:54 PM | 显示全部楼层
shern91 发表于 5-10-2013 12:46 PM
谢谢你哦...问你几个问
aircond1.switchOn();
aircond2.switchOn();

你家有两架aircond, 你只开了第一架,第二架会自动开咩?

那个 “!" 的意思是 if not, 整句的意思 if not isOn 就。。。


本帖最后由 黑木头 于 5-10-2013 12:55 PM 编辑

回复

使用道具 举报

 楼主| 发表于 5-10-2013 12:56 PM | 显示全部楼层
黑木头 发表于 5-10-2013 12:54 PM
你家有两架aircond, 你只开了第一架,第二架会自动开咩?

那个 “!" 的意思是 if not, 整句的意思 ...

哈哈,完全明白了...谢谢...三个object就是三架冷气...
回复

使用道具 举报

 楼主| 发表于 5-10-2013 01:55 PM | 显示全部楼层
黑木头 发表于 5-10-2013 12:54 PM
你家有两架aircond, 你只开了第一架,第二架会自动开咩?

那个 “!" 的意思是 if not, 整句的意思 ...

第三题是不是我应该做switch case来给user选择加减乘除那样,过后再System.out.print() call method,我已经做好加跟减了..只是
  
memsto(float x)
  
void
Stores x into internal memory
memclr()
void
Resets  internal memory (to zero)
memget()
float
Returns value  in internal memory
memadd(float x)
void
Adds x to value in internal memory and  stores the result into internal memory.
sqroot(float x)
float
Calculates  square root of x and  returns the result.
power(float x, float y)
float
Calculates xy and returns the result.

a.             Inputs:              floatx, y, z
Expression:      file:///C:/Users/lenovo/AppData/Local/Temp/msohtmlclip1/01/clip_image002.gif
b.    Inputs:              float k, g, m, x
Expression:      x+(((m-(k-g))+x)-g)
c.              Inputs:              int t, x, y
                                    float k, g
Expression:      file:///C:/Users/lenovo/AppData/Local/Temp/msohtmlclip1/01/clip_image004.gif
d.             Inputs:              intx, z, m, n
Expression:      file:///C:/Users/lenovo/AppData/Local/Temp/msohtmlclip1/01/clip_image006.gif
4.     Based on the Calculator class in the previous question, determine what the followingprograms calculate.
a.              
calc.memclr();
     calc.memsto(calc.add(x, y));
     calc.memadd(calc.times(z, y));
     System.out.println(calc.memget());
b.
            calc.memclr();
     calc.memsto(calc.add(calc.times(2,x), 1));
     calc.memsto(calc.power(calc.add(y,z), calc.memget()));
     calc.memadd(calc.memget());
     System.out.println(calc.memget());a.             Inputs:              floatx, y, z
Expression:      file:///C:/Users/lenovo/AppData/Local/Temp/msohtmlclip1/01/clip_image002.gif
b.    Inputs:              float k, g, m, x
Expression:      x+(((m-(k-g))+x)-g)
c.              Inputs:              int t, x, y
                                    float k, g
Expression:      file:///C:/Users/lenovo/AppData/Local/Temp/msohtmlclip1/01/clip_image004.gif
d.             Inputs:              intx, z, m, n
Expression:      file:///C:/Users/lenovo/AppData/Local/Temp/msohtmlclip1/01/clip_image006.gif
4.     Based on the Calculator class in the previous question, determine what the followingprograms calculate.
a.              
calc.memclr();
     calc.memsto(calc.add(x, y));
     calc.memadd(calc.times(z, y));
     System.out.println(calc.memget());
b.
            calc.memclr();
     calc.memsto(calc.add(calc.times(2,x), 1));
     calc.memsto(calc.power(calc.add(y,z), calc.memget()));
     calc.memadd(calc.memget());
     System.out.println(calc.memget());



不是很明白,是不需要program的吗?
回复

使用道具 举报


ADVERTISEMENT

发表于 5-10-2013 09:43 PM | 显示全部楼层
shern91 发表于 5-10-2013 01:55 PM
第三题是不是我应该做switch case来给user选择加减乘除那样,过后再System.out.print() call method,我已 ...

我没有去看你的题目,我认为你应该找你的同学讨论,然后试试一起写出来,错了就改,再错了再改,那样你才可以学到东西.



回复

使用道具 举报

 楼主| 发表于 5-10-2013 09:47 PM | 显示全部楼层
黑木头 发表于 5-10-2013 09:43 PM
我没有去看你的题目,我认为你应该找你的同学讨论,然后试试一起写出来,错了就改,再错了再改,那样你才 ...

我看过我同学的答案,他们做的program好像prototype那样,就是看了好像能通,可是放下去要debug就完全不能了...问他们怎样做他们说也是抄人的而已...
回复

使用道具 举报

 楼主| 发表于 6-10-2013 12:34 PM | 显示全部楼层
SotongJiang 发表于 6-10-2013 12:49 AM
黑木兄出手解决了冷气机那题,虽然我和之前楼上几位都刻意不给你完整的code。
希望你和同学能分析他的co ...

你是要给我自己解决问题,可是我领悟能力低,黒木兄进出手了...哈哈...
回复

使用道具 举报

 楼主| 发表于 6-10-2013 02:06 PM | 显示全部楼层
SotongJiang 发表于 6-10-2013 12:49 AM
黑木兄出手解决了冷气机那题,虽然我和之前楼上几位都刻意不给你完整的code。
希望你和同学能分析他的co ...

我的老师是叫不用做programing...可是我又看到那边写write a program,老师叫我们写认为的output,好像aircondtest那题,不过我还是决定做program多学一些东西好像我之前不大会的boolean...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 15-9-2025 11:24 PM , Processed in 0.136001 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表