Title: Classes part 1
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

Classes part 1

Basics

1. Create a Date class with:
variables: day, month, year
Methods : setDate, setDay, setMonth, setYear, writeFormat1 ( 9/20/48) , writeFormat2 (September 20, 2009)
2. We want to highlight: class, objects, methods, instance variables, new, "A Class is a type"
3. Accessing instance variables and methods from some place else like main. Accessing instance variables and methods within the class itself.
4. Highly recommend the class name being the same as the Filename with .java tacked on the end.
5. Formal parameter names and actual parameters used in calling methods. Note that in general data types of calling parameters must match.

MyDate.java



public class MyDate {
    private int year=2012;
    private int month = 1;
    private int day =1;

    void setDate(int y, int m, int d)
    {
        year = y;
        month = m;
        day = d;

    }
    public int getYear()
    {
        return year;
    }
    public void setYear(int y)
    {
        year = y;
    }
    String getFormattedDate()
    {
        return month + "/" + day + "/"+ year;
    }
}


MyDateDemo.java



public class MyDateDemo {

    public static void main(String[] args) {

        MyDate md1 = new MyDate();
        md1.setDate(2013, 4, 20);

        String s = md1.getFormattedDate();
        System.out.println(s);

        MyDate md2 = new MyDate();
        md2.setDate(1953, 11, 3);

        //md1.year = 1980;
        md1.setYear(1980);



    }
}



Other Class Example: VendingMachine with:

instance variables:

- location
- mmsCount
- chipsCount
- mmsPrice
- chipsPrice
- money

methods:
- boolean buy(String type)
- int addInventory(String type, int num)
- void setLocation(String )
- int getMoney()
- void printStatus()

VendingMachine.java



public class VendingMachine {

    private String location=" unknown";
    private int mmsCount = 0;
    private int chipsCount =0;
    private int mmsPrice= 85;
    private int chipsPrice = 95;
    private int money =0;

    void printStatus()
    {
        System.out.println(" Status: money="+ money + " #chips="+ chipsCount +
                " #mms= "+ mmsCount);
    }

    public void setLocation(String loc)
    {
        location = loc;
    }
    public int getMoney()
    {
        return money;
    }

    public boolean buy(String type)
    {
        if (type.equals("chips"))
        {
            if (chipsCount > 0)
            {
                chipsCount -= 1;
                money += chipsPrice;
                return true;
            }
            else
                return false; // no inventory
        }
        if (type.equals("mms"))
        {
            if (mmsCount > 0)
            {
                mmsCount -= 1;
                money += mmsPrice;
                return true;
            }
            else
                return false; // no inventory
        }
        System.out.println("Bad buy attempt ... illegal type="+type);
        return false;
    }

    public int addInventory(String type, int count)
    {
        if (type.equals("chips"))
        {
            chipsCount += count;
            return chipsCount;
        }
        if (type.equals("mms"))
        {
            mmsCount += count;
            return mmsCount;
        }
        System.out.println("Bad Inventory type: " + type);
        return 0;
    }



}




VendingMachineDemo.java



public class VendingMachineDemo {


    public static void main(String[] args) {

        VendingMachine vm1 = new VendingMachine();
        vm1.addInventory("chips", 30);
        vm1.addInventory("mms", 40);
        vm1.setLocation("Student Center");


        VendingMachine vm2 = new VendingMachine();
        vm2.addInventory("chips", 30);
        vm2.addInventory("mms", 40);
        vm2.setLocation("BE Building");

        if (vm1.buy("chips"))
            System.out.println("Bought chips");

        if (vm2.buy("mms"))
            System.out.println("Bought MMS");

        vm1.printStatus();
        vm2.printStatus();

    }

}



Java Practice(JPHK):

1. (5 points) Add the MyDate class to your project as it shows up above. Create a class named TestMyDate that contains a main method where you will do the following work.
  1. Construct a MyDate object name m1. Call setDate for m1 to give it a value of year=1984, month=5, day=16.
  2. Construct another MyDate object named m2. Call setYear for m2 to give it a year of 1999.
  3. Call getFomattedDate for m1 and save the result in a String named s1.
  4. Call getFormattedDate for m2 and save the result in a String named s2.
  5. Print out the results of s1 and s2.
2. (5 points) Create a class named TestVendingMachine that contains a main method. Add the VendingMachine class shown above to your project.
  1. In the main method of your TestVendingMaching class, construct a VendingMachine object with a variable named firstFloor.
  2. Call setLocation with a parameter of "First Floor of BE" for the firstFloor object.
  3. For the firstFloor object, call addInventory with type="chips" and count=5
  4. For the firstFloor object, call addInventory with type="mms" and count=4
  5. Create a while loop that will buy "chips" from the firstFloor object until you run out of inventory (buy will return a false).
  6. Now call the getMoney method of the firstFloor object and print out the amount returned from getMoney.
  7. Now call the printStatus routine of the firstFloor object.

Last Modified: December 6, 2014 2:35 PM