Java classes in action

Java extra constructs

We introduced lists from Java collections as well as saw some examples of StringBuilder usages:

  • defining lists and using them:
    final ArrayList<Integer> lArrayList = new ArrayList<Integer>();
    final List<Integer> lInterface = new ArrayList<Integer>();
    final var lShort = new ArrayList<Integer>();
    
    lShort.add(42);
    for (final var i : lShort) {
        System.out.println(i);
    }
    
  • basic usage of StringBuilder class:
    final var sb = new StringBuilder();
    sb.append("Hello StringBuilder world")
      .append("\n")
      .append("I've numbers:");
    for (int i = 0; i < 42; i++) sb.append(" ").append(i); 
    return sb.toString();
    

Java tasks

We were working on some tasks related to designing Java classes

  1. Implement Fraction class. It should provide basic operations on fractions as well as some good way of printing its representation.

    Sample solution

     public class Fraction {
    
       private static final Fraction NEG_ONE = new Fraction(-1, 1);
    
       private final int numerator;
       private final int denominator;
    
       public Fraction(int numerator, int denominator) {
         if (denominator == 0) throw new IllegalArgumentException("denominator cannot be 0");
         final var gcd = gcd(denominator, numerator);
         this.numerator = numerator / gcd;
         this.denominator = denominator / gcd;
       }
    
       public Fraction add(Fraction other) {
         final var den = this.numerator * other.denominator + other.numerator * this.denominator;
         final var num = this.denominator * other.denominator;
         return new Fraction(den, num);
       }
    
       public Fraction subtract(Fraction other) {
         final var negOther = other.multiply(NEG_ONE);
         return this.add(negOther);
       }
    
       public Fraction multiply(Fraction other) {
         final var num = this.numerator * other.numerator;
         final var den = this.denominator * other.denominator;
         return new Fraction(num, den);
       }
    
       public Fraction divide(Fraction other) {
         return other.inverse().multiply(this);
       }
    
       @Override
       public String toString() {
         final var num = Math.abs(this.numerator);
         final var den = Math.abs(this.denominator);
         final var isPositive = this.numerator * this.denominator >= 0;
         return (isPositive ? "" : "-") + num + "/" + den;
       }
    
       private Fraction inverse() {
         return new Fraction(this.denominator, this.numerator);
       }
    
       private static int gcd(int x, int y) {
         if (y != 0) return gcd(y, x % y);
         else return x;
       }
     }
    

  2. Design and implement Student class with its system to manage students state in academic years.

    Sample solution

     public class Program {
       private final String faculty;
       private final String major;
       private int year;
    
       public Program(String faculty, String major, int year) {
         this.faculty = faculty;
         this.major = major;
         this.year = year;
       }
    
       public Program(String faculty, String major) {
         this(faculty, major, 1);
       }
    
       public Program(Program program) {
         this(program.faculty, program.major, program.year);
       }
    
       public void incrementYear() {
         this.year += 1;
       }
    
       public String getFaculty() {
         return faculty;
       }
    
       public String getMajor() {
         return major;
       }
    
       public int getYear() {
         return year;
       }
    
       @Override
       public String toString() {
         return year + " year of " + major + " at " + faculty;
       }
     }
    
     public class Student {
    
       private final String firstname;
       private final String lastname;
       private final String number;
       private final List<Program> programs;
    
       public Student(String firstname, String lastname, String number, List<Program> programs) {
         this.firstname = firstname;
         this.lastname = lastname;
         this.number = number;
         this.programs = programs;
       }
    
       public Student(String firstname, String lastname, String number) {
         this(firstname, lastname, number, new ArrayList<>());
       }
    
       public Student(String firstname, String lastname, String number, Program program) {
         this(firstname, lastname, number, singleProgramList(program));
       }
    
       public String getFullName() {
         return firstname + " " + lastname;
       }
    
       public void addProgram(Program program) {
         this.programs.add(program);
       }
    
       public void incrementYear() {
         for (final var program : programs) program.incrementYear();
       }
    
       public List<Program> getPrograms() {
         final var result = new ArrayList<Program>();
         for (final var program : programs) result.add(new Program(program));
         return result;
         // alternative one-liner code with Java Streams:
         // return programs.stream().map(Program::new).collect(Collectors.toList());
       }
    
       @Override
       public String toString() {
         final var sb = new StringBuilder();
         sb.append("--- Student ").append(getFullName()).append(" ---\n")
           .append("index: ").append(number).append("\n")
           .append("is attending:");
    
         for (final var program : programs) sb.append("\n").append("  - ").append(program);
    
         return sb.append("\n").toString();
       }
    
       private static List<Program> singleProgramList(Program program) {
         final var result = new ArrayList<Program>();
         result.add(program);
         return result;
       }
     }
    

Previous
Next