09-24-2013 - HW

posted Sep 24, 2013, 8:40 AM by Samuel Konstantinovich   [ updated Sep 25, 2013, 5:20 AM ]
Method Madness!

Return types 
  • can be any primitive, type, or class
  • for a method that does not need a return value, the type is void
  • Void methods can still use return to signal the end of a method
  • if a method has a non-void return type, you must have return value at the end
  • all variables must be declared before being used 
  • if a variable is declared as a primitive type, you have to initialize it before performing any operations 
  • if a variable is declared as an object type, it must be instantiated before being used
  • you instantiate objects with the constructor
  • to call methods on an object, use the period.
  • =  is the assignment operator, not the comparison operator.


  • string is a group of one or more characters
  • Java has a preset String class (note the capital S), so literals are not necessary
  • To declare, use String just like a primitive type. However, Java has two ways of instantiating strings.
    • eg. String s, t, u;
      • s = new String();
      • t = new String ("yo");
      • u = "hi";
        • note that this third example treats strings as though they were primitives. This looks like assignment of values but is really constructing u, as the first two examples do.
  • Strings can be combined using +.
    • eg. u = s + "hi";
Note: You can have multiple methods with the same name if parameters are set different, also known as overloading.

Special Methods

Main method
  • Every java program must have one main method
  • Only method that must run when you run a program
  • Header:  public static void main(string[ ] args)
  • creates an instance of a class
  • constructor name is the same as the class name
  • often instance variables are given initial values in the constructor
Note: If you don't write a constructor, java will automatically create a default one.

//1. Look up if statements
//2. Modify the greeting class using if statements to behave as described below
//3. Write a main to test your class. 

public class greeter{
    private String message;
    private String mood;

    public greeter(){
message = "Hello.";
  mood = "Neutral";//Choose between: "Happy" "Neutral" "Sad"

    public greeter(String s){
message = s;
        mood = "Neutral";

    public greeter(String mess, String moo){
message= mess;
mood = moo;

    public String getGreeting(){
//Homework: use if statements to alter the greeting returned depending on the mood.
        //lets say the message="Hello."
        //Neutral: normal greeting (like "Hello.")
//Happy:  the normal greeting + a happy sentence (like "Hello. Wheeeeeee!")
        //Sad:    the normal greeting + a sad sentence (like "Hello. Life is terrible.")

        return message;

//  When comparing strings you cant say name=="bob", this DOES NOT WORK!
//  Strings require the use of   .equals()  
//  "Basdf".equals("hey")  is false, (assume String a="hey") a.equals("hey")   is true
//  name.equals("bob") checks if the value of name is "bob"