CS 1073 Introductory Programming
|
| Version with one class, including main function | Version with main function in separate class |
|---|---|
// Rectangle.java: the rectangle class
public class Rectangle{
// data fields: usually private
private int length, width;
// constructor, must be public, no parameters
public Rectangle(){
length = 0;
width = 0;
}
// constructor, must be public, two parameters
public Rectangle(int len, int wid){
length = len;
width = wid;
}
// accessors, usually public, return data members
public int getLength(){ return length; }
public int getWidth(){ return width; }
// other functions, called methods
public int getArea(){ return length * width; }
public int getPerim(){ return 2*length + 2*width; }
// special toString function
public String toString(){
return "Rectangle. width: " + width +
", length: " + length +
", area: " + getArea() +
", Perim: " + getPerim();
}
public static void main(String args[]) {
int l, w;
Rectangle r1 = new Rectangle();
System.out.println(r1); // calls toString of r1
Rectangle r2 = new Rectangle(4,3);
System.out.println(r2); // calls toString of r2
System.out.println("Test getLength, getWidth ...");
l = r2.getLength();
w = r2.getWidth();
System.out.println(" r2 length: " + l);
System.out.println(" r2 width: " + w);
System.out.println("Test getArea, getPerim ...");
System.out.println(" r1 area: " + r1.getArea());
System.out.println(" r1 Perim: " + r1.getPerim());
System.out.println(" r2 area: " + r2.getArea());
System.out.println(" r2 Perim: " + r2.getPerim());
}
}
|
// Rectangle.java: the rectangle class
public class Rectangle{
// data fields: usually private
private int length, width;
// constructor, must be public, no parameters
public Rectangle(){
length = 0;
width = 0;
}
// constructor, must be public, two parameters
public Rectangle(int len, int wid){
length = len;
width = wid;
}
// accessors, usually public, return data members
public int getLength(){ return length; }
public int getWidth(){ return width; }
// other functions, called methods
public int getArea(){ return length * width; }
public int getPerim(){ return 2*length + 2*width; }
// special toString function
public String toString(){
return "Rectangle. width: " + width +
", length: " + length +
", area: " + getArea() +
", Perim: " + getPerim();
}
}
======== separate file: RectangleMain.java =============
// RectangleMain: holds main for Rectangle class
public class RectangleMain {
public static void main(String args[]) {
int l, w;
Rectangle r1 = new Rectangle();
System.out.println(r1); // calls toString of r1
Rectangle r2 = new Rectangle(4,3);
System.out.println(r2); // calls toString of r2
System.out.println("Test getLength, getWidth ...");
l = r2.getLength();
w = r2.getWidth();
System.out.println(" r2 length: " + l);
System.out.println(" r2 width: " + w);
System.out.println("Test getArea, getPerim ...");
System.out.println(" r1 area: " + r1.getArea());
System.out.println(" r1 Perim: " + r1.getPerim());
System.out.println(" r2 area: " + r2.getArea());
System.out.println(" r2 Perim: " + r2.getPerim());
}
}
|
Here are results of a run. Both versions produce the same output.
Rectangle. width: 0, length: 0, area: 0, Perim: 0 Rectangle. width: 3, length: 4, area: 12, Perim: 14 Test getLength, getWidth ... r2 length: 4 r2 width: 3 Test getArea, getPerim ... r1 area: 0 r1 Perim: 0 r2 area: 12 r2 Perim: 14
| Sheep Class |
|---|
public class Sheep {
private static int count = 0;
private String name;
public Sheep (String sName) {
name = sName;
count++;
}
public static int getCount( ) {
return count;
}
public String getName( ) {
return name;
}
public String sayBaa( ) {
return "Baa " + name;
}
}
|
Exercise: Write code in a main function to create 3 Sheep and output the count. Compare the use of static versus non-static qualifiers. Answer: The static getCount can be referenced through the class name, Sheep. The non-static getName and sayBaa are referenced by object.
| In the main function |
|---|
Sheep s1 = new Sheep("gracie");
Sheep s2 = new Sheep("george");
Sheep s3 = new Sheep("gerald");
System.out.println("We have " + Sheep.getCount() + " sheep");
System.out.println("Sheep " + s1.getName() + " says " + s1.sayBaa());
|
// Student.java: a simple class holding information about a student
public class Student {
private String name; // In the form "LastName, FirstName"
private double GPA; // grade point average
private int collegeClass; // 1 = FR, 2 = SO, 3 = JR, 4 = SR, 5 = GR
public Student(String n, double gpa, int c) { // constructor
name = n; GPA = gpa; collegeClass = c;
}
public double getGPA() { // return the GPA
return GPA;
}
public String toString() {
return "Name: " + name + ", GPA: " + GPA +
", Class Code: " + collegeClass;
}
}
// Students.java: test the Student class
public class Students {
public static void main (String[] args) {
Student jBlow = new Student("Blow, Joe", 3.4, 2);
Student bBonkers = new Student("Bonkers, Bruce", 2.9, 3);
Student nNutcase = new Student("Nutcase, Nancy", 3.7, 2);
System.out.println(jBlow + "\n" + bBonkers + "\n" + nNutcase);
System.out.println("GPA of JoeBlow: " + jBlow.getGPA());
}
}
The output:Name: Blow, Joe, GPA: 3.4, Class Code: 2 Name: Bonkers, Bruce, GPA: 2.9, Class Code: 3 Name: Nutcase, Nancy, GPA: 3.7, Class Code: 2 GPA of JoeBlow: 3.4
A fourth example: the Rational class: See Rational class. See also long Rational class