Tag Archives: OOP

Class Roster Array Version [Java] Assignment

Class Roster: This class will implement the functionality of all roster for school. It will , in essence, manage an Array of Student objects. Each roster will store a default of 10 Students

  • “global” Instance Variables
    • private Student myStudents   : an array storing Student Objects
    • private String className  (a name to represent a given roster like “Ap CS”)
    • public int period //during which period of the day does this roster meet
  • a default constructor should initialize the Array to store 20 strings
    a single parameter constructor that takes an Array <String>
    Both constructors should initialize the instance variables
  • Methods
    • Accessors
      • private int indexOf(Student st) //@returns index of Student St or -1
      • public boolean containsStudent(Student studentName )/ /@returns true if studentName is in roster .
      • public boolean equals(ClassRoster other) //@ returns whether or not rosters are equal. Rosters are equal if identical students are in identical order in the lists
    • Mutators:

public void addStudent(String studentName, int age) ;// adds student name and age to end of roster

. Hint: Make user of the containsStudent() method
public boolean removeStudent(int ssnId ) // removes student from roster based on id. Make sure that you maintain the integrity of the parallel ArrayLists.

public boolean removeStudent(String name) //removes student based on name. Make sure that you maintain the integrity of the parallel ArrayLists.

public  boolean removeStudent( Student st) //removes student st. Make sure that you maintain the integrity of the parallel ArrayLists.

Student Object (OOP Java)

Student Class

A school district decided to write a program to help keep track of various parts of the school.

It has been decided to store relevant information about Students in a Student class. Each Student will have an array of Strings to store the names of his or her teachers.

private instance variables


Part 1

* Construct the Java statement to declare the array called

teachersNames to store the String names of the student’s teachers

Part 2

  • write a default constructor
  • construct a method called  addTeacher( String name) that adds String name to the  teachersNames  array
    • to do this let’s create a private method  private String[] addStr( String[] input, String val) that adds value to the end of the array and returns it. [redact]Solution[/redact]
  • construct hasTeacher(String name)  that returns true if name is in the teachersName array
  • construct a method removeTeacher(String name) that removes the   String name from the  teachersNames  array.
  • construct a method the line of code needed in the setGpa(double to) method of the Student Class.
  • construct a method boolean setTeacher (String name, int index) .   This method should return true if index is valid in the array and false if  index >= teachersName.length


Next: The runner class

Playlist Project [Array]

A PlayList project will be based on Three Classes

The Artist class will store core information about an artist

The Song class will store basic information about a music song

The PlayList class will manage lists of songs and artists



  • Constructor(s) :
    public Artist( String name) { //missing code }


  • Instance Variables (“global” variables)







Instance Variables

example of how to get current milliseconds as a long









  • Instance Variables
    • private String listName : This is the ‘name’ of your playList
    • private Song[] songs;
    • private  int[] stars ; //how many stars each song has between 0 and 5 inclusive.
    • Note: songs and stars are parallel Arrays
  • Constructor
    • public PlayList(String name) : There should be only 1 constructor that takes a parameter representing the name of the playList
  • Accessor Methods
    • public double averageRating() // returns the average star rating for the list
    • public double averageRating(Artist artist)  // returns the mean star rating associated with artist
    • public Song[] getSongs(Artist artist) // returns an array populated by the songs of the parameter artist
    • public Song[] getSongs() // returns all the songs in the list

    Mutator Methods

    • public void swap(Song song1 ,  Song song2 )  // switches positions of these two (maintain parallelism!)
    • public void  add(Song song , int stars)
    • public void removeSong(Song song, String artist )  //removes song associated with artist
      • Note: Be careful here. You are allowed to add the same song and artist. Duplicates are allowed.
    • public void removeArtist(Artist artist ) //removes all elements associated with artist
    • public void removeLowStars(int cutOff) //removes all elements associated with a star rating less than cutOff
    •  ** public PlayList sortByDate() //this returns a rearranged PlayList based on each Song’s date
    • ** public PlayList sortByRating() //this returns a rearranged playlist so that the 5 starred elements are the first group in the list, 4 stars second …1 stars, last
    • ** public PlayList shuffle() //this returns a new PlayList in which all of the songs have been reordered randomly.




Account Abstract Class Assignment

This is the first part of a 2 part project.

Person Class


  • public Person( String _name, int _age)

instance Variables

  • private String name
  • private int age


  • public String  getName()
  • public int compareTo(Person other) { return this.age - other.age; }


Account – Abstract Super Class

Static Variable(s)

  • private static int nextAccountNum
  • private static int parentCompanyCode = 12810  ;


  • public Account( double_balance, Person _owner)

instance Variables

  • private int  accountNumber
  • protected  double  balance
  • protected  ArrayList<Person> owners


  • public static int getParentCode()
  • public int getAccountNumber()
  • public void deposit(double amount)
  • public double getCurrentBalance()
  • public abstract  boolean withdraw(double amount)
  • public boolean equals(Account other) // are the unique account numbers equals ?
  • public String toString()
  • public int comparesTo(Account other) { return this.accountNumber- other.accountNumber }  (Note that this method has an ‘s‘ )
  • public void addOwner(Person p) //adds P to the ArrayList of owners
  • public ArrayList<Person> getOwners()  //  returns the ArrayList of owners
  • public Person remove( Person p) // this emulates the ArrayList’s remove() method (link) . Remember that that method removes the object and then returns the removed object. In this case, you should remove Person P from the ArrayList of owners

  • CheckingAccount extends Account
  • This class has to do certain things. You can decide the best way to do it. This class should have a way to
    • attempt to withdraw money by writing a check
    • public void writeCheck(double amount)
    • public void cancelCheck(int checkNumber)
      • be able to cancel checks and keep track of the check numbers that have been cancelled
      • when a check is cancelled the amount associated with that check is returned back to the balance
  • One solution
    • every time you write a check, you need a new check “#” (static variable like an id)
    • writeCheck( double amount) - > tries to call this.withdraw(double amount)  as long as the balance is enough and records a new check number by adding it to an ArrayList<Integer>  that stores the check numbers of cancelled checks. It would also need a parallel ArrayList<Double>  to store balances (in case the check is cancelled and the amount is refunded to the balance)

SavingsAccount extends Account

  • instance variables
    • private double interestRate
    • private ArrayList<Double> dailyBalances

    static variables

    • public static final int MINIMUM_BALANCE=100;  // this is the absolute minimum amount of money that must always be in the account
  • methods
    • public boolean withdraw(double amount)  //@override the withdraw() method to ensure that currentBalance the never gets below MINIMUM_BALANCE
    • public void recordDailyBalance()  // @ record current day’s balance by storing current balance.
    • public double projectBalance(double timeInYears)  // use the compound interest formula .  Assume that the bank compounds the balance monthly. To learn more about how compound interest really works, click here .
    • public  void updateInterestPayment() // @ once a month update account based on average daily balances and interest rate

CertificateOfDeposit extends SavingsAccount

instance Variables

  • public static final  double EARLY_WITHDRAW_PENALTY= 200 .


  • public boolean withdraw(double amount , boolean isEarly )  //@override the withdraw()  and applies penalty to balance

When you are done, you can begin the Bank project .

Super Classes Are Great I


Question: What limitations seem to exist with our use of classes?


I. Implement all Methods of the Triangle and Rectangle methods below



Class: Rectangle  download-bttnRectangle


  • public Rectangle ( int  _col,  double  _width,  double _height)

instance Variables

  • private int color;
  • private double width;
  • private double height ;

Accessor Methods

  • public double getArea(); //returns the area of the rectangle
  • public int getColor()


Class: RightTriangle  download-bttnRightTriangle


  • public RightTriangle( int _col,  double  _width,  double _height)

instance Variables

  • private int color;
  • private double width;
  • private double height;

Accessor Methods

  • public double getArea(); //returns the area of the triangle
  • public int getColor()
  • public double getHypotenuse() ;

Class: InteractiveMath

download-bttn InteractiveMath-Arraylists (USE THIS ONE)

instance Variables

  • ArrayList<Rectangle> rectangles;
  • ArrayList<RightTriangle> triangles;

Accesor Methods

  • public double computeNetArea(){ //@ returns the area of the rectangles and triangles


Next  : Super Classes are great 2

Then  : constructors and inheritance.

after that : What is “Inherited” by Subclasses?