Você está na página 1de 6

Take home Exam 4

Amira Osama 20153201


Question 1:

1. Line 1: Private: Should be: Public to give the class a package scope.
2. Line 2: 3.14: Should be 3.14F.
3. Line 8: this.r: Should be r only.
4. Line 11: double in the construction: Should be removed: Constructions don’t have a return type.
5. Line 15: Missing the closing brace for the constructor “ } ”.
6. Line 18: r^2: Should be: PI*r*r as the symbol ^ means XOR in java.
7. Line 21: Missing the closing of the comment */.
8. Line 22: Missing the type od the variable in the parameters for y1 which should be a double.
9. Line 23: dx has the type int but calculates floats so it should have a casting.
10. Line 24: dy has the type int but calculates floats so it should have a casting.
11. Line 29: String should be Strings.
12. Line 31: int i; is defined already in line 30.
13. Line 32: Missing the starting braces for the loop for { .

Question 2:

Pair Class:

 Private int i;
 Instance variable, accessed inside the class only.
 Private String s:
 Instance variable, accessed inside the class only.
 Public Pair(int I, String s):
 Method in the stack for the class pair.
 Int i:
 Local variable for the constructor.
 String s:
 Local variable for the constructor.
 This.i = I;
 References the int i in the constructor to the instance variable in the class private inti i;
 This s = s;
 References the String s in the constructor to the instance variable in the class private
String s;
Octopus Class:

 Main( ):
 Method in the stack.
 Octopus o = new Octopus();
 Create an object from the type Octopus and reference to it by o .
 O.test( );
 Calls the method test in the class Octopus for the object o.
 declares local variables int j;
 declares local variable String t;
 declares an object Pair with reference p;
 Initialize j = 42;
 Initialize t = “hello Mr. squid”; in the String pool.
 Initialize the object p to the constructor Pair( j , t);
 References the j and t to the constructor parameters.

Question 3:

a. Time Class.
b. Book Class.
c. Person Class:

Person
+ Name: String.
+ Age: int.

Dancer DountMaker
+ Company: String + Shop: String.
Time.java

public class Time {


private int second, minute, hour;

public Time(int second, int minute, int hour) {


this.second = second;
this.minute = minute;
this.hour = hour;
}

public Time() {
this.second = 0;
this.minute = 0;
this.hour = 0;
}

public int getSecond() {


return this.second;
}

public int getMinute() {


return this.minute;
}

public int getHour() {


return this.hour;
}

public void setSecond(int second) {


this.second = second;
}

public void setMinute(int minute) {


this.minute = minute;
}

public void setHour(int hour) {


this.hour = hour;
}

public String toString() {


return String.format("%02d:%02d:%02d", hour, minute, second);
}

public void setTime(int second, int minute, int hour) {

this.second = second;
this.minute = minute;
this.hour = hour;
}

Page 1
Time.java

public Time nextSecond() {


++second;
if (second >= 60) {
second = 0;
++minute;
if (minute >= 60) {
minute = 0;
++hour;
if (hour >= 24) {
hour = 0;
}
}
}
return this;
}

public Time previousSecond() {


--second;
if (second == 0) {
second = 59;
--minute;
if (minute == 0) {
minute = 59;
--hour;
if (hour == 0) {
hour = 23;
}
}
}
return this;
}

Page 2
Book.java

public class Author {

private String name;


private String email;
private char gender

public Author(String name, String email, char gender) {


this. name = name;
this. email = email;
this. gender = gender;
}

public String toString() {


return "Author [ name= " + name + ", email= " + email + ", gender= " + gender +
"]";
}
}

public class Book {

private String name;


private Author author;
private double price;
private int qty = 0;

public Book (String name, Author author, double price) {


this. name = name;
this. author = author;
this. price = price;
}

public Book (String name, Author author, double price, int qty) {
this. name = name;
this. author = author;
this. price = price;
this. qty = qty;
}

public String getName () {


return name;
}

public Author getAuthor () {


return author;
}

public double getPrice () {


return price;
}

public int getQty () {


return qty;
}

public void setPrice (double price) {


this.price = price;
}

Page 1
Book.java

public void setQty (int qty) {


this.qty = qty;
}

public String toString () {


return "Book [ name = " + name + author.toString() + ", price= " + price + ",
qty= " + qty + "]" ;
}
}

Page 2

Você também pode gostar