THINK FIRST·CODE LATER

← Java Programming

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 1231 · 16 labs

A1 Chapter 8 Medium

MyTriangle: methods, validation and formatted output

Write a class MyTriangle with two static methods: isValid(double s1, double s2, double s3), returning true when the three lengths can form a triangle...

A2 Chapter 9 Medium

findMode: the most frequent value of a sorted array

Write public static int findMode(int[] array) returning the value that occurs most often in a sorted array. If several values tie, return the first. T...

A3 Chapter 9 Medium

leastFrequent: the rarest element of an unsorted array

Write public static int leastFrequent(int[] arr) returning the element that occurs the fewest times; if several tie, return the one appearing earliest...

A4 Chapter 9 Medium

Matrix addition

Compute and print C = A + B for A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] and B = [[9, 8, 7], [6, 5, 4], [3, 2, 1]].

L3.1 Chapter 3 Easy

Sum and average of three numbers

Write a class Numbers that reads three integers from the keyboard and prints: - their sum, as Sum: 12 - their average with two decimal places, as Aver...

L4.1 Chapter 4 Easy

Seconds into hours, minutes and seconds

Write a class TimeSplit that reads a number of seconds (a positive int) and prints it as hours, minutes and seconds, exactly in this form: [code] Use...

L5.1 Chapter 5 Easy

Initials and vowels in a name

Write a class NameTool that reads a full name on one line (for example hamza djigal) and prints: - the initials in capitals, as Initials: H.D. - the n...

L6.1 Chapter 6 Easy

Letter grade with validation

Write a class Grade that reads a score (an int) and prints the letter grade: Score Grade --- --- 90–100 A 80–89 B 70–79 C 60–69 D 0–59 F Print it as G...

L6.2 Chapter 6 Medium

Days in a month (leap years)

Write a class DaysInMonth that reads a year and a month (1–12) and prints the number of days, as Days: 29. A year is a leap year when it is divisible...

L7.1 Chapter 7 Medium

Multiplication table

Write a class Multiplication that prints the multiplication table from 1 to 9 as a neat grid, using nested loops. Each number must be printed in a fie...

L7.2 Chapter 7 Medium

Reading until a sentinel

Write a class Sentinel that reads integers until the user types -1, then prints: [code] The sentinel -1 is not part of the numbers. If no number was e...

L8.1 Chapter 8 Medium

Prime numbers

Write a class Primes with a method [code] that returns true when n is a prime number (1 and every number below it are not prime). The main method read...

L8.2 Chapter 8 Medium

gcd and lcm

Write a class MathTools with two static methods: [code] gcd returns the greatest common divisor (use Euclid's method: while b is not 0, replace the pa...

L9.1 Chapter 9 Medium

Statistics of an array

Write a class Stats that reads a count n, then n integers into an array, and prints: [code] Write the minimum and the maximum with your own loop — do...

L9.2 Chapter 9 Medium

Reverse an array and search in it

Write a class ArrayTools with two static methods: [code] The main method reads a count n, then n integers, then one value to search for. It prints the...

L9.3 Chapter 9 Hard

Row and column sums of a matrix

Write a class MatrixSums that reads two integers rows and cols, then rows × cols integers into a two-dimensional array, and prints the sum of each row...

CPS 2231 · 18 labs

A5 Chapter 10 Medium

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...

A6 Chapter 11 Medium

MyClass: completing an instance method

Complete the program so that it prints 1 and then 2.

A7 Chapter 16 Medium

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...

A8 Chapter 16 Medium

MessageTooLongException: a custom unchecked exception

Write an unchecked exception and a Messenger class whose send refuses messages longer than 160 characters.

A9 Chapter 16 Medium

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...

A10 Chapter 12 Medium

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...

A11 Chapter 15 Medium

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...

A12 Chapter 17 Medium

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.

L10.1 Chapter 10 Easy

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()...

L11.1 Chapter 11 Medium

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...

L12.1 Chapter 12 Medium

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...

L13.1 Chapter 13 Medium

ArrayList of students

Write a class Student with private fields name and grade (int), a constructor, getName(), getGrade() and toString() returning name (grade). StudentLis...

L13.2 Chapter 13 Easy

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...

L14.1 Chapter 14 Medium

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...

L15.1 Chapter 15 Hard

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...

L16.1 Chapter 16 Medium

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...

L17.1 Chapter 17 Medium

Recursion: factorial, digits and Fibonacci

Write a class Recursion with three recursive methods (no loops): [code] main reads one integer n and prints: [code]

L17.2 Chapter 17 Hard

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...