2016-09-21 HW05

posted Sep 21, 2016, 6:20 AM by Samuel Konstantinovich   [ updated Sep 21, 2016, 7:18 PM ]
Goal:  Objects in Java.

Continuation of the greeter:

Greeter.java

public class Greeter{
    //instance variable (member field, member variable)
    public String name;
    public int age;

    //constructors
    public Greeter(String myName, int myAge){
name = myName;
age = myAge;
    }
    
// the default constructor is the contructor with no parameters
    public Greeter(){
name = "Jon";
age = 21;
    }

    //member methods
    public void greet(Greeter other){
System.out.println("Hello "+other.name+", my name is "+ name + " and I am "+age+" years old.");
    }

    public void greet(){
System.out.println("Hello, my name is "+ name + " and I am "+age+" years old.");
    }
}

Driver.java
public class Driver{
    public static void main(String[]args){
Greeter g1,g2,g3;
//calling different constructors
g1 = new Greeter("LoneStar",42);
g2 = new Greeter();
g3 = new Greeter("Dark Helmet",46);

//calling different greet methods
g1.greet(g3);
g2.greet();
g3.greet();
}
}


HW05: (due Friday 8am on homework server.)
Do what you can tonight, and we will go over some more after the quiz tomorrow.
Student object!!!

1. Make a class to test your Student class that has a main. Call it Driver for now. As you add more pieces to your Student class, test as you go in Driver.java.

2. Now make a simple Student Class that has the following:
a) Instance variables:
firstName
lastName
osis
age
gpa

b)constructors:
Student(first,last,ID,years)

c)methods:  (you decide which methods require parameters and which do not)
void printStudent()   //print out the student in some way to the console.
void lowerGPA()  //lower the GPA by some amount
void raiseGPA() //increase the GPA by some amount 
Comments