Você está na página 1de 7

Advanced Programming Takehome Exam 2

Answer all of the following questions, with justification for your answers. Send your answers in the form
of a word or PDF document to my email: amira.abdelaziz@fue.edu.eg maximum by Wednesday night.

1- Implement a class called circle is designed as following:


• Two private instance variables: radius (of the type double) and color (of the type String).
• Two overloaded constructors - a default constructor with no argument, which initializes radius and color
with default value of 1.0 and "red", respectively. And another constructor which takes a double
argument for radius, and a color.
• Two public methods: getRadius(), getColor(), and getArea(), which return the radius, the color, and the
calculated area of this instance, respectively (the area of a circle is calculated as the square of radius
multiplied by PI).
• Two public methods: setRadius( ) and setColor( ) methods, which set values for radius and color
respectively.
• A public method toString ( ) that returns a string representing the values of the circle.

The final design of the class Circle should be like this:

Solution:

public class Circle {


private double radius;
private String color;

public Circle() {
radius = 1.0;
color = "red";
}

public Circle(double r, String c) {


radius = r;
color = c;
}
public double getRadius() {
return radius;
}

public String getColor() {


return color;
}

public double getArea() {


return (Math.PI * radius * radius);
}

public void setRadius(double r) {


radius = r;
}

public void setColor(String c) {


color = c;
}

public String toString() {


return "Circle [radius="+radius+", color="+color+”]”;
}
}

2- In the same manner, implement a class Rectangle following the next diagram:

Solution:
public class Rectangle {
private float length, width;

public Rectangle() {
length = 1.0f;
width = 1.0f;
}

public Rectangle(float l, float w) {


length = l;
width = w;
}

public void setLength(float l) {


length = l;
}

public void setWidth(float w) {


width = w;
}

public float getWidth() {


return width;
}

public float getLength() {


return length;
}

public double getArea() {


return width*length; //optional
}

public double getPerimeter() {


return 2*width + 2*length; //optional
}

public String toString() {


return "Rectangle [length="+length+", width="+width=”]”;
}
}

3- Implement a class Employee following the next diagram:


Solution:

public class Employee {


private int id;
private String firstName;
private String lastName;
private int salary;

public Employee(int i,String fn,String ln,int s) {


id = i;
firstName = fn;
lastName = ln;
salary = s;
}

public int getID() {


return id;
}

public String getFirstName() {


return firstName;
}

public String getLastName() {


return lastName;
}

public int getSalary() {


return salary;
}

public void setSalary(int s) {


salary = s;
}

public int getAnnualSalary() {


return salary*12;
}

public int raiseSalary(int percent) { //implementation is optional


salary += (salary*percent/100);
return salary;
}

public String toString() {


return "Employee[id="+id+", name="+firstName+" "+lastName+", salary="+salary+"]";
}
}
Select the appropriate answer(s) for the following questions (with justification):

Q1. Which of the following are keywords or reserved words in Java?

1) if
2) then
3) goto
4) while
5) case

Q2. Which of the following are legal identifiers?

1) 2variable
2) variable2
3) _whatavariable
4) _3_
5) $anothervar
6) #myvar

Q3. If you run the code below, what gets printed out?

String s=new String("Bicycle");


int iBegin=1;
char iEnd=3;
System.out.println(s.substring(iBegin,iEnd));

1) Bic
2) ic
3) icy
4) error: no method matching substring(int,char)

Q4. Given the following declarations

String s1=new String("Hello")


String s2=new String("there");
String s3=new String();

Which of the following are legal operations?

1) s3=s1 + s2;
2) s3=s1-s2;
3) s3=s1 & s2;
4) s3=s1 && s2

Q5. Given the following code what will be output?

public class Pass{


static int j=20;
public static void main(String argv[]){
int i=10;
Pass p = new Pass();
p.amethod(i);
System.out.println(i);
System.out.println(j);
}

public void amethod(int x){


x=x*2;
j=j*2;
}
}

1) Error: amethod parameter does not match variable


2) 20 and 40
3) 10 and 40
4) 10, and 20

Q6. Which of the following statements are true?

1) The garbage collection algorithm in Java is vendor implemented


2) The size of primitives is platform dependent
3) The default type for a numerical literal with decimal component is a float.

Q7. Which of the following statements are true?

1) The default constructor has a return type of void


2) The default constructor takes a parameter of void
3) The default constructor takes no parameters
4) The default constructor is not created if the class has any constructors of its own.

Q8. What will happen if you compile/run the following lines of code?

1: int[] iArray = new int[10];


2:
3: iArray.length = 15;
4:
5: System.out.println(iArray.length);

A) Prints 10.
B) Prints 15.
C) Compilation error, you can't change the length of an array.
D) Runtime exception at line 3.

Q9. If you compile and execute an application with the following code in its main() method:

String s = new String( "Computer" );


if( s == "Computer" )

System.out.println( "Equal A" );

if( s.equals( "Computer" ) )

System.out.println( "Equal B" );


1) It will not compile because the String class does not support the = = operator.
2) It will compile and run, but nothing is printed.
3) "Equal A" is the only thing that is printed.
4) "Equal B" is the only thing that is printed.
5) Both "Equal A" and "Equal B" are printed.

Q10. Given the variable declarations below:

byte myByte;
int myInt;
long myLong;
char myChar;
float myFloat;
double myDouble;

Which one of the following assignments would need an explicit cast?

1) myInt = myByte;
2) myInt = myLong;
3) myByte = 3;
4) myInt = myChar;
5) myFloat = myDouble;
6) myFloat = 3;
7) myDouble = 3.0;

Você também pode gostar