Programming Lab 3 : classes, objects, data types and input/output

CSCI-UA 9102, Data Structures

Exercise 1

• Exercise 1.1. We consider the class ‘Progression’ below together with its child, FibonacciProgression with implements the series F[n] = F[n-1] + F[n-2]. Give a short fragment of Java code that uses those progression classes to find the eighth value of a Fibonacci progression that starts with 2 and 2 as its first two values.
``````
public class Progression{

// instance variable
protected long current;

/** Constructs a progression starting at zero*/
public Progression() {this(0);}

/** Constructs a progression with a given start value.*/
public Progression(long start){current = start;}

/** Returns the next value of the progression. */
public long nextValue(){
long answer = current;

}
/** Advances the current value to the next value of the progression */
current++;
}

/** Prints the next n values of the progression, separated by spaces. */
protected void printProgression(int n){
System.out.print(nextValue());   // print first value without leading space
for(int j=1; j < n; j++)
System.out.print(" " + nextValue()); // print leading space before others
System.out.println(" ")    // end the line

}

}
``````

``````
public class FibonacciProgression extends Progression{

protected long prev;

/** Constructs traditional Fibonacci starting 0,1,1,2,3,...*/
public FibonacciProgression() {this(0,1);}

/** Constructs generalized Fibonacci, with given first and second values.*/
public FibonacciProgression(long first, long second){
super(first);
prev = second - first; // fictitious value preceding the first

}

/** Replaces (prev, current) with (current, current+prev). */
long temp = prev;
prev = current;
current += temp;

}
}
``````

• Exercise 1.2. Consider the code fragment below, taken from some package. What is the output of calling the main() method of the Marylabd class?

``````
public class Maryland extends State {
Maryland(){/* null constructor */}
public void printMe() {System.out.println("Read it.")}
public static void main(String[] args){
Region east = new State();
State md = new Maryland();
Object obj = new Place();
Place usa = new Region();
md.printMe();
east.printMe();
((Place) obj).printMe();
obj = md;
((Maryland) obj).printMe();
obj = usa;
((Place) obj).printMe();
usa = md;
((Place) usa).printMe();

}}
``````

``````
class State extends Region {
State(){ /* null constructor */}
public void printMe() {System.out.println("Ship it. ");}
}
``````

``````
class Region extends Place {
Region(){ /* null constructor */}
public void printMe() {System.out.println("Box it. ");}
}
``````

``````
class Place extends Object {
Place(){ /* null constructor */}
public void printMe() {System.out.println("Buy it. ");}
}
``````

• Exercise 1.3.

Write a program that consists of three classes, A, B and C. such that B extends A and that C extends B. Each class should define an instance variable named "x" (that is each class has its own variable named x). Describe a way for a method in C to access and set A's version of x to a given value, without changing B or C's version.

• Exercise 1.4. Modify the advance method of the FibonacciProgression class above so as to avoid use of any temporary variable.
• Exercise 1.5.

Define a Polygon interface that has a method area() and perimeter(). Then implement classes for Triangle, Quadrilateral, Pentagon, Hexagon and Octagon which implement this interface with the obvious meanings for the area() and perimeter() methods. Also implement classes IsoscelesTriangle, EquilateralTriangle, Rectangle and Square, which have the appropriate inheritance relationships. Finally, write a simple user interface, which allows users to create polygons of the various type, input their geometric dimensions, and then output their area and perimeter. For extra effort, allow users to input polygons by specifying their vertex coordinates and be able to test if two such polygons are similar.

• Exercise 1.6. Consider the class Student below. Modify this class as follows. Each student object should also contain the scores for three tests. Provide a constructor that sets all instance values based on parameter values. Overload the constructor so that each test score is assumed to be initially zero. Provide a method called 'setTestScore' that accepts two parameters: the test number (1 through 3) and the score. Also provide a method called 'getTestScore' that accepts the test number and returns the appropriate score. Provide a method called 'average' that computes and return the average test score for this student. Modify the 'toString' method so that the test scores and average are included in the description of the student. Modify the driver class (i.e. the class StudentBody) below to exercise the new 'Student' methods

``````
public class Student{
private String firstName, lastName;

//--------------------------------------------------------------
// Constructor : Sets up the student with the specified values
//--------------------------------------------------------------
public Student (String first, String last, Address home, Address school){

firstName = first;
lastName = last;
}
//--------------------------------------------------------------
// returns a string description of the student object
//--------------------------------------------------------------
public String toString(){
String result;

result = firstName + " " + lastName + "\n";
result += "Home Address: \n" + homeAddress + "\n";
result += "School Address: \n" + schoolAddress;

return result;

}}
``````

``````
//=============================================================
// Authors: Lewis, Loftus
//=============================================================

public class StudentBody{
// --------------------------------------------------------------
// Creates some Address and Student objects and prints them
// --------------------------------------------------------------
public static void main(String[] args){
Address school = new Address("800 Lancaster Ave. ", "Villanova", "PA", 19085);
Address jHome = new Address("800 Lancaster Ave. ", "Villanova", "PA", 19085);
Student john = new Address("800 Lancaster Ave. ", "Villanova", "PA", 19085);

}}
``````

• Exercise 1.7. Write a short Java program that takes as input three integers a,b and c and from the Java console, and determines if they can be used in a correct arithmetic formula (in the given order) like "a+b=c", "a=b-c" or "a*b=c"