Java Program - Action Event. Need Help ...

  • Thread starter Thread starter NhoMt
  • Ngày gửi Ngày gửi

NhoMt

<font color=#ff00ff>Dễ Thương</font>
Tham gia ngày
16/4/03
Bài viết
5,669
Reaction score
11
Thứ 4 trả bài .....
Giúp iêm với ....
Góp ý nhanh nha


Which ONE of the following is TRUE?
a. A subclass may delete instance variables that are declared in its superclass.
b. A subclass may declare new instance variables that are not declared in its superclass
c. A subclass may delete messages that are defined in its superclass
d. A subclass must use all of the methods defined in its superclass.

/////////////////////////////////

Consider a private static variable called votingAge declared in a class called Person. Assume that the following expressions are in an instance method in the Person class. Which ONE of the expressions would generate a compiler error?
a. System.out.println(Person.votingAge);
b. System.out.println(this.votingAge);
c. System.out.println(votingAge);
d. Person.votingAge = 21;

/////////////////////////////////

What is the ActionEvent class?


What is a static method ?


In writing the code for a method, a programmer might use the keyword this. When the method is invoked from somewhere in the program, what does this refer to?


What is the effect of writing final when declaring a variable reference?

/////////////////////////////////////////////////////////////////


Consider a class called Part. Every Part has a name, and a price. The name and price are set when the Part is created. Consider another class called an Assembly. An assembly has a name, exactly 3 parts, a date, a total price and a discount percentage. The name, the discount percentage and all three parts must be specified when the assembly is created. The date of the assembly is the date it was created. The total price of the assembly is the sum of the prices of the parts. The discounted price of the assembly is the total price less the discount. When a part is asked to display itself it looks like the following:
Engine $6000
When an assembly is asked to display itself it looks like:
Automobile
assembled on 12/1/2006
consists of
Engine $6000
Frame $7000
Interior $4000
Total $17000
Discount 10%
Discounted Price $15300
Here is the code for the classes Part and Assembly, along with a program class called Test that creates an assembly and displays it. Fill in the ???? with the missing code.

////////////////////////////////////////

public class Assembly {
/* Instance Variables */
private String name;
private Date today;
private int discount;
//define instance variables named parta, partb, partc
private ????;
private ????;
private ????;
/*
Each of the instances represents an Assembly that has a name, date, a discount percentage and three parts.
*/
public Assembly(String assemblyName,
int m, int d, int y,
int percentage,
????, ????, ???? ) {
/*Initialize object to have the given name, discount rate, date and the given three
parts (3 points)*/

????
????
????
????
????
????
}
public int price() {
/*Return my total un-discounted price in dollars.*/
return ??? + ???? + ????;
}
public int discountedPrice() {
/*Return my discounted price in dollars.*/
double price;
price = ?????? ;
return (int)(price);
}
public void display() {


System.out.println(???);//display name
System.out.print("\t assembled on");
System.out.println(????);//display date
System.out.println("\t consists of");
System.out.print("\t");
this.part1.display();
System.out.print("\t");
this.part2.display();
System.out.print("\t");
this.part3.display();
System.out.print("\tTotal $");
System.out.println(???);//display price
System.out.print("Discount ");
System.out.print(????);//display discount
System.out.println("%");
System.out.print("Discounted price $");
System.out.println(????);//display discounted price
}

}
////////////////////////////////////////////

public class Part {

/* Instance Variables */
private String name;
private int price;


public Part(String partName, int value) {
/*Initialize object to have the given name and price in dollars.*/
????
????
}
public void display() { //
/*Display part name*/
System.out.print(????);

System.out.print(" $");

/*Display part price*/
System.out.println(????);
}
public int price() {
/*Return price in dollars. */
???????
}

}
/////////////////////////////////////////////////
public class Test {
/*
Test the Assembly and Parts classes by creating an assembly and displaying it.
*/
public static void main(String args[]) {
/*
Create the 3 part objects part1, part2, part3, create the an assembly object that contains them and
display the assembly. */
//Create object part1 with the values "Engine", 6000
??????
//(Create object part2 with the values"Frame", 7000;
?????
//Create object part3 with the values"Interior", 4000;
?????

//Create object assembly with the values"Automobile", 12,1,2006, 10, part1, part2, part3;
??????????

assembly.display();
}
}
/////////////////////////////////////////////////
//Date.java

// Declaration of the Date class.
public class Date {
private int month; // 1-12
private int day; // 1-31 based on month
private int year; // any year
// Constructor:
public Date( int mn, int dy, int yr )
{ month = mn;
day = dy;
year = yr;
}
// Create a String of the form month/day/year
public String toString()
{ return month + "/" + day + "/" + year; }
}
 
Which ONE of the following is TRUE?
a. A subclass may delete instance variables that are declared in its superclass.
b. A subclass may declare new instance variables that are not declared in its superclass
c. A subclass may delete messages that are defined in its superclass
d. A subclass must use all of the methods defined in its superclass.

/////////////////////////////////

Consider a private static variable called votingAge declared in a class called Person. Assume that the following expressions are in an instance method in the Person class. Which ONE of the expressions would generate a compiler error?
a. System.out.println(Person.votingAge);
b. System.out.println(this.votingAge);
c. System.out.println(votingAge);
d. Person.votingAge = 21;

/////////////////////////////////

Câu 1: chọn "lớp con có thể khai báo biến chưa có trên lớp cha".
Câu 2: viết thử 1 chương trình có biến mang thuộc tính private static rồi thử xem cái nào lỗi.

Consider a class called Part. Every Part has a name, and a price. The name and price are set when the Part is created. Consider another class called an Assembly. An assembly has a name, exactly 3 parts, a date, a total price and a discount percentage. The name, the discount percentage and all three parts must be specified when the assembly is created. The date of the assembly is the date it was created. The total price of the assembly is the sum of the prices of the parts. The discounted price of the assembly is the total price less the discount. When a part is asked to display itself it looks like the following:
Engine $6000
When an assembly is asked to display itself it looks like:
Automobile
assembled on 12/1/2006
consists of
Engine $6000
Frame $7000
Interior $4000
Total $17000
Discount 10%
Discounted Price $15300
Here is the code for the classes Part and Assembly, along with a program class called Test that creates an assembly and displays it. Fill in the ???? with the missing code.

Mã:
package untitled1;

class Assembly {
  /* Instance Variables */
  private String name;
  private Date today;
  private int discount;
//define instance variables named parta, partb, partc
  private Part part1;
  private Part part2;
  private Part part3;

  public Assembly(String assemblyName,
                  int m, int d, int y,
                  int percentage,
                  Part partA , Part partB , Part partC ) {
    name = assemblyName;
    today = new Date(m, d, y);
    discount = percentage;
    part1 = partA;
    part2 = partB;
    part3 = partC;
  }

  public int price() {
    /*Return my total un-discounted price in dollars.*/
    return part1.price() + part2.price() + part3.price();
  }

  public int discountedPrice() {
    /*Return my discounted price in dollars.*/
    double price;
    price = price() * (1 - discount);
    return (int)(price);
  }

  public void display() {
    System.out.println(name); //display name
    System.out.print("\tAssembled on");
    System.out.println(today); //display date
    System.out.println("\tConsists of");
    System.out.print("\t");
    this.part1.display();
    System.out.print("\t");
    this.part2.display();
    System.out.print("\t");
    this.part3.display();
    System.out.print("\tTotal $");
    System.out.println(price()); //display price
    System.out.print("Discount ");
    System.out.print(discount); //display discount
    System.out.println("%");
    System.out.print("Discounted price $");
    System.out.println(discountedPrice()); //display discounted price
  }

}

class Part {
  private String name;
  private int price;

  public Part(String partName, int value) {
    name = partName;
    price = value;
  }

  public void display() {
    /*Display part name*/
    System.out.print(name);

    System.out.print(" $");

    /*Display part price*/
    System.out.println(price);
  }

  public int price() {
    return price;
  }
}

class Date {
  private int month; // 1-12
  private int day; // 1-31 based on month
  private int year; // any year
  //Constructor
  public Date(int mn, int dy, int yr) {
    month = mn;
    day = dy;
    year = yr;
  }

  // Create a String of the form month/day/year
  public String toString() {
    return month + "/" + day + "/" + year;
  }
}

class Test {

  //Test the Assembly and Parts classes by creating an assembly and displaying it.

  public static void main(String args[]) {
  /*Create the 3 part objects part1, part2, part3, create the an assembly object that contains them and
  display the assembly. */
//Create object part1 with the values "Engine", 6000
    Part partA = new Part("Engine", 6000);
//(Create object part2 with the values"Frame", 7000;
    Part partB = new Part("Frame", 7000);
//Create object part3 with the values"Interior", 4000;
    Part partC = new Part("Interior", 4000);
//Create object assembly with the values"Automobile", 12,1,2006, 10, part1, part2, part3;
    Assembly assembly = new Assembly("Automobile", 12, 1, 2006, 10, partA, partB, partC);
    assembly.display();
  }
}

What is the ActionEvent class?
Lớp lưu trữ các thông tin của sự kiện action.

In writing the code for a method, a programmer might use the keyword this. When the method is invoked from somewhere in the program, what does this refer to?
http://java.sun.com/docs/books/tutorial/java/javaOO/thiskey.html

What is the effect of writing final when declaring a variable reference?
You can declare a variable in any scope to be final. The value of a final variable cannot change after it has been initialized. Such variables are similar to constants in other programming languages.
To declare a final variable, use the final keyword in the variable declaration before the type.

final int aFinalVar = 0;

The previous statement declares a final variable and initializes it all at once. Subsequent attempts to assign a value to aFinalVar result in a compiler error. You may, if necessary, defer initialization of a final local variable. Simply declare the local variable and initialize it later, as follows.
final int blankfinal;
. . .
blankfinal = 0;

A final local variable that has been declared but not yet initialized is called a blank final. Again, once a final local variable has been initialized, it cannot be set, and any later attempts to assign a value to blankfinal result in a compile-time error.
 
Back
Top