Question: 1. Given the GeometricObject and Triangle classes below: public class GeometricObject { private String color = white; private boolean filled; private java.util.Date dateCreated; /** Construct

1. Given the GeometricObject and Triangle classes below:

public class GeometricObject {

private String color = "white";

private boolean filled;

private java.util.Date dateCreated;

/** Construct a default geometric object */

public GeometricObject() {

dateCreated = new java.util.Date();

}

. . .

}

public class Triangle extends GeometricObject

And the following declarations.

Triangle tri1 = new Triangle ();

GeometricObject go1 = new GeometricObject();

circle all the Boolean expressions that are true below.

i) (tri1 instanceof GeometricObject)

ii) (go1 instanceof GeometricObject)

iii) (tri1 instanceof Triangle)

iv) (go1 instanceof Triangle)

b) Given the declaration of tri1 above, can the following statement be compiled? Why or why not?

GeometricObject go2 = tri1;

2. Given the following class declarations that are in the same package,

public class myclass1 {

public double dvar1;

protected double dvar2;

double dvar3;

private double dvar4;

protected void m1() {}

private void m2(){}

public void m3() {}

myclass1 () {}

}

public class myclass2 extends myclass1 {

...

}

a) List the member variables and methods in myclass1 that are accessible in myclass2.

b) Which methods in myclass1 can be overriden in myclass2?

c) Can the method m3() in myclass1 be declared a private method in myclass2? Explain

3 a)Given the code below, circle the statements that are true:

Circle circ1 = new Circle(0.5);

Circle circ2 = circ1;

i) circ2 and circ1 have the same contents

ii) circ2 and circ1 reference the same location in memory but have different contents

iii) circ1 and circ1 have the same contents but reference different locations in memory

b) The Circle class has an overriden method:

public boolean equals(Object obj) {

if (obj instanceof Circle) {

return this.radius == ((Circle)obj).radius;

}

else

return false;

}

i) How would circ1 use the equals() method to determine if circ2 has the same contents in an expression.

ii) In the method above, what does the this keyword represent?

iii) Why is the Circle cast used in the equals() method above?

4a) You are given a class named Clock that has one int instance variable called hours. Write a constructor with no parameters for the class Clock. The constructor should set hours to 12.

Clock() {hours=12;}

b) Write the definition of a class Telephoneb. The class has no constructors or other methods, one instance variable of type String called number, and two static variables. One is of type int called quantity, initialized to 250; the other is of type double called total, initialized to 15658.92.

Public class Telephoneb

{

String number;

static int quantity = 250;

static double total = 15658.92;

}

c) Write the definition of a class Telephonec. The class has no constructors and one static method getFullNumber(). The method accepts a String parameter and returns a String object, adding 718- to the beginning of the String parameter.

Public class Telephonec

{

Static String getFullNumber (String pn)

{

String rv = 718-+pn;

Return rv;

}

}

5. Consider a class called MenuItem with three member variables, a String, name, that holds the name of a menu item, e.g. Hamburger, a double, price, that is the price of the item, and an int, calories, that is the number of calories in the food. The class has methods:

o At least one constructor

o Mutator and accessor methods

o a method, addTax(), which receives a double parameter that is the tax rate. e.g. 0.0875 and returns the item price plus the sales tax

Assuming that the member variables are private, declare the MenuItem class.

public class MenuItem

{

private String name;

private double price;

private int calories;

MenuItem()

{

name = ;

price = 0;

calories = 0;

}

public String getName() { return name;}

public double getPrice() {return price;}

public int getCalories() {return calories;}

public void setName(String nm) {name = nm;}

public void setPrice(double pr) { price = pr;}

public void setCalories(int cal) { calories = cal;}

public double addTax(double rate)

{

return price + (price*rate);

}

}

Does the MenuItem class represent a has-a or a is-a relationship? Explain

It has a has-a because it has a String object name; It does not extend any class so it does not inherit from any class so not an is-a relation.

6.A class Menu is planned that will create a list of MenuItem objects.

a) Should the Menu class have has-a relationship or inherit from the MenuItem class? Explain your reasoning.

b) How would you include multiple MenuItem objects in a Menu object.? Your answer should be consistent with your answer in a).

c) Give a description of the member variables and methods that are needed in the Menu class based on your answers to a) and b) above.

has-a because it should should have multiple MenuItem objects.

Include an array of MenuItem objects

Items an array of MenuItems

n an int saying how many items in the array

addItem(name, price, calories) adds a new MenuItem;

getPrice(nm) get the price of the MenuItem with name nm

7. Write the definition of the Menu class from 6.c) including at least one constructor.

Public double getPrice(String nm)

{

for (int i = 0; i

if (nm.equals(items[i].getName()))

return items[i].getPrice();

return 0;

}

void addItem(String nm, double pr, int cal)

{

items[n] = new menuItem();

items[n].setName(nm);

items[n].setPrice(pr);

items[n].setCalories(cal);

n++;

}

8. Given the Phonebook class from the homework:

public class Phonebook {

public static void main(String [] args) throws Exception {

final int CAPACITY = 100;

String []

lnames = new String[CAPACITY],

fnames = new String[CAPACITY],

numbers = new String[CAPACITY];

. . }

Write a method lookup that is passed two strings first and last and returns that persons number, if they are found else returns null;

Additional:

Be able to sum an array, compute the average of an array of integers, Find the minimum or maximum integer in array.

Strings.

Be able to find the occurrence of some pattern within a String e.g. find the position of all occurences of cat within a string. In other words be able to use substring and indexOf

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!