Programming Lab 3 : classes, objects, data types and input/output
CSCI-UA 9102, Data Structures
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;
advance();
return answer;
}
/** Advances the current value to the next value of the progression */
protected void advance(){
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). */
protected void advance(){
long temp = prev;
prev = current;
current += temp;
}
}
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. ");}
}
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.
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.
public class Student{
private String firstName, lastName;
private Address homeAddress, schoolAddress;
//--------------------------------------------------------------
// Constructor : Sets up the student with the specified values
//--------------------------------------------------------------
public Student (String first, String last, Address home, Address school){
firstName = first;
lastName = last;
homeAddress = home;
schoolAddress = school;
}
//--------------------------------------------------------------
// 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);
}}