有JAVA高手吗,能帮我解答几题吗,万分感谢

[复制链接]
查看11 | 回复4 | 2008-2-13 12:43:03 | 显示全部楼层 |阅读模式
(1)The following is invalid. Why?
public void myMethod(int one) {
return (one + one);
}

(2)Which of the following methods are class methods?
public static void one( ) { . . . }
publicinttwo ( ) { . . . }
private static int three ( ) { . . . }
public float four ( ) { . . . }

(3)If the data member feeRate is declared private, is the following statement valid? Why?
FeeCharge = currencyChanger.feeRate;

(4)Do Exercise 4 of Chapter 4 (page 183).
Determine the output of the given program.

Hint: draw the state of memory diagram to solve the problem.
/*
Chapter 4 Exercise 4
*/
import javabook.*;
class Exercise4_4
{
public static void main( String[ ] args )
{
Question4 q4;
q4 = new Question4( );
q4.start( );
}
}
class Question4
{
private int x, y, z;
private MainWindow mainWindow;
private OutputBoxoutputBox;

public void start ( )
{
intx, y;
setup();
x = y = 10;
modify(x, y);
printout();
}
private void setup( )
{
mainWindow = new MainWindow( );
outputBox= new OutputBox(mainWindow);

mainWindow.setVisible( true );
outputBox.setVisible( true );
x = 100;
y = 200;
z = 300;
}
private void modify( int x, int y)
{
z = x + y;
x = z;
y = 2 * z;
}
private void printout( )
{
outputBox.printLine("x = " + x);
outputBox.printLine("y = " + y);
outputBox.printLine("z = " + z);
}
}
(5)Write an application that computes the area of a donut region (the shaded area) given the radius of the inner and outer circles.
Guidelines:
1,Define an instantiable Donut class which accepts the radius of the inner and outer circles when creating the Donut object. The Donut class should include the following:
-
private instance variable innerRadius
-
private instance variable outerRadius
-
a proper constructor
-
private instance method circleArea with one parameter radius. The method returns the area of the circle of specified radius.
-
public instance method getDonutArea with no parameter. The method returns the area of the donut by using the method circleArea.
2,Write an application that accepts the inner radius and outer radius of the donut from the user, and output the area of the donut.

(6.)
Refer to Ex 13 in chapter 4. Write an instantiable WeightConverter class. An instance of this class is created by passing the gravity of an object relative to the earth’s gravity. For example, the moon’s gravity is approximately 0.167 of the earth’s gravity, so we create a WeightConverter instance for the moon as
WeightConverter
moonWeight;
moonWeight = new WeightConverter(0.167);

An instance of this class can also be created without specifying the relative gravity, but assigned at a later time as

WeightConverter
PlanetWeight;

planetWeight = new WeightConverter( );

. . .

planetWeight.setFactor(1.3);

To compute how much you wiegh on the moon, you pass your wieght on earth to the convert method as

yourMoonWeight = moonWeight.convert( 160 );

Define the WeightConverter class. Write an application that inputs the user’s weight on earth and outputs his/her weight on Mercury, Venus, Jupiter, Saturn and a planet of the user’s choice. (See Exercise 12 of chapter 3, page 132) Use the values in the table below:

Planet
Multiply the earth Weight by

Mercury
0.4

Venus
0.9

Jupiter
2.5

Saturn
1.1

Your planet
input your own factor

Guidelines:
A demonstration of the program is given as WeightConverterMain.class and WeightConverter.class in folder apLab7. You may also refer to the CurrencyConverter example in chapter 4.

Deadline:
You have to finish Question 6 BEFORE the next lab session and your program will be inspected in the next lab session.
回复

使用道具 举报

千问 | 2008-2-13 12:43:03 | 显示全部楼层
最初由 5692066 发布
[B](1)The following is invalid. Why?
public void myMethod(int one) {
return (one + one);
}
[/B]

(one + one)是int类型,
而此方法声明为void了。
回复

使用道具 举报

千问 | 2008-2-13 12:43:03 | 显示全部楼层
最初由 5692066 发布
[B](2)Which of the following methods are class methods?
public static void one( ) { . . . }
publicinttwo ( ) { . . . }
private static int three ( ) { . . . }
public float four ( ) { . . . }
[/B]

one和three方法。
回复

使用道具 举报

千问 | 2008-2-13 12:43:03 | 显示全部楼层
(3)If the data member feeRate is declared private, is the following statement valid? Why?
FeeCharge = currencyChanger.feeRate;
外部类可以访问内部类中的私有变量
回复

使用道具 举报

千问 | 2008-2-13 12:43:03 | 显示全部楼层
1.because it's return type is void.
2.public int two();
public float four()
3.currencyChanger's class is singlenton class .currencyChanger is itself's member.feeCharge is its member.
4.100,200,20
5.import java.math.*;
public class DonutArea{

private int innerRadius;
private int outerRadius;

DonutArea(int innerRadius,int outerRadius){
this.innerRadius=outerRadius;
this.outerRadius=outerRadius;
}
public double getInnerArea(){

return Math.PI*innerRadius*innerRadius;
}
public double getOuterArea(){

return Math.PI*outerRadius*outerRadius;
}
public double getDonutArea(){
int temp;
if(innerRadius>outerRadius){

temp=innerRadius;

innerRadius=outerRadius;

outerRadius=temp;
}
temp=outerRadius-innerRadius;
return Math.PI*(temp)*(temp);
}
}
6. public class WeightConverter {

private double factor ;
public WeightConver(double factor){

setFactor();
}
public void setFactor(double factor){

this.factor=factor;
}
public double convert(double weight){
return weight*factor;
}

}
回复

使用道具 举报

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

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行