Programming labs
Write the program yourself, then compare with the model solution. The problems follow the chapters, from the first programs to object-oriented design.
CPS 2231 · 18 labs
Student and StudentApp: class design
Design a Student class with attributes id, name, age and grade; a default and a parameterised constructor; getters and setters for all attributes; dis...
MyClass: completing an instance method
Complete the program so that it prints 1 and then 2.
TimeFormatException: a custom checked exception
Write a checked exception TimeFormatException and a method parseTime(String) that accepts hh:mm, returns the hours and minutes, and throws the excepti...
MessageTooLongException: a custom unchecked exception
Write an unchecked exception and a Messenger class whose send refuses messages longer than 160 characters.
TripComputer: throwing and multi-catch
Write averageSpeed(double distance, double hours) that rejects a negative distance with an IllegalArgumentException and zero hours with an ArithmeticE...
BankAccount: encapsulation and a class counter
Write a BankAccount class with a private balance, a static counter of accounts created, an automatically assigned account number, deposit, withdraw th...
Shape hierarchy: abstract class, polymorphism and an interface
Write an abstract Shape with area() and perimeter(), subclasses Circle, Rectangle and IsoscelesRightTriangle, and an interface Scalable implemented by...
GradeReport: file input, objects and file output
Read students.txt (lines of the form id,name,grade), build Student objects, and write a formatted report.txt ending with the class average.
Rectangle: a first class with accessors
Write a class Rectangle with two private instance variables width and height (double), a constructor taking both, accessors getWidth() and getHeight()...
Book: overloaded constructors and equality
Write a class Book with private fields title (String), author (String) and year (int), and: - a constructor with the three values; - a second construc...
Utility class and object counter
Write a class Temperature with: - a private field celsius; - a static counter of how many Temperature objects were created, readable with public stati...
ArrayList of students
Write a class Student with private fields name and grade (int), a constructor, getName(), getGrade() and toString() returning name (grade). StudentLis...
StringBuilder: reverse and palindrome
Write a class Palindrome with a method [code] that returns true when the text reads the same in both directions, ignoring case, spaces and punctuation...
Employee and Manager: inheritance
Write a class Employee with private name and salary (double), a constructor, getName(), getSalary(), a method raise(double percent) that increases the...
Payable interface and polymorphism
Write an interface Payable with one method double pay(). Write two classes implementing it: - HourlyWorker(String name, double hours, double rate) — p...
InsufficientFundsException
Write a checked exception InsufficientFundsException whose message is Missing 50.00, where the number is the amount that is lacking. Write a class Acc...
Recursion: factorial, digits and Fibonacci
Write a class Recursion with three recursive methods (no loops): [code] main reads one integer n and prints: [code]
Words report from text
Write a class WordReport that reads one line of text and prints a small report: [code] - Words counts the words (separated by spaces); - Longest is th...