May 11, 2023

Brute Force Techniques I

This is a description

Building, House & School

- rootdir
  - com
    - course
      - structure
        - Building.java
        - House.java
        - School.java
        - Main.java
// Building.java
package com.course.structure;

public class Building {
    // ... (unchanged)
}

// House.java
package com.course.structure;

public class House extends Building {
    // ... (unchanged)
}

// School.java
package com.course.structure;

public class School extends Building {
    // ... (unchanged)
}

// Main.java
package com.course.structure;

public class Main {
    public static void main(String[] args) {
        House house = new House(3200, 2, 4);
        School school = new School(20000, 5, 20, "Elementary");
    }
}

Max Static Overloading

- rootdir
  - myPackages
    - p1
      - Maximum.java
      - Main.java
// Maximum.java
package myPackages.p1;

public class Maximum {

    public static int max(int a, int b, int c) {
        return Math.max(Math.max(a, b), c);
    }

    public static double max(double a, double b, double c) {
        return Math.max(Math.max(a, b), c);
    }

    public static int max(int[] arr) {
        int max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
        }
        return max;
    }

    public static int max(int[][] matrix) {
        int max = matrix[0][0];
        for (int[] row : matrix) {
            for (int element : row) {
                if (element > max) {
                    max = element;
                }
            }
        }
        return max;
    }
}

// Main.java
package myPackages.p1;

public class Main {
    public static void main(String[] args) {
        int maxInt = Maximum.max(3, 8, 2);
        System.out.println("Maximum among integers: " + maxInt);

        double maxDouble = Maximum.max(3.5, 8.2, 2.7);
        System.out.println("Maximum among doubles: " + maxDouble);

        int[] arr = {12, 5, 8, 9, 2, 15, 7};
        int maxInArray = Maximum.max(arr);
        System.out.println("Maximum in the array: " + maxInArray);

        int[][] matrix = {
            {4, 8, 2},
            {3, 9, 7},
            {1, 6, 5}
        };
        int maxInMatrix = Maximum.max(matrix);
        System.out.println("Maximum in the matrix: " + maxInMatrix);
    }
}

Series Interface

interface Series {
    int getNext();
    void reset();
    void setStart(int start);
}

class ByTwos implements Series {
    private int start;
    private int current;

    public ByTwos() {
        this.start = 0;
        this.current = 0;
    }

    @Override
    public int getNext() {
        current += 2;
        return current;
    }

    @Override
    public void reset() {
        current = start;
    }

    @Override
    public void setStart(int start) {
        this.start = start;
        this.current = start;
    }
}

public class Main {
    public static void main(String[] args) {
        ByTwos series = new ByTwos();

        series.setStart(4);

        System.out.println("First number in the series: " + series.getNext());
        System.out.println("Next number: " + series.getNext());
        System.out.println("Next number: " + series.getNext());

        series.reset();

        System.out.println("After resetting, next number: " + series.getNext());
    }
}

Sports Interface

interface Sports {
    void putGrade();
}

class Student {
    private int rollNumber;
    private int marks;

    public Student(int rollNumber, int marks) {
        this.rollNumber = rollNumber;
        this.marks = marks;
    }

    public int getRollNumber() {
        return rollNumber;
    }

    public void putMarks(int marks) {
        this.marks = marks;
    }

    public int getMarks() {
        return marks;
    }
}

class Result extends Student implements Sports {
    private int sportsGrade;

    public Result(int rollNumber, int marks, int sportsGrade) {
        super(rollNumber, marks);
        this.sportsGrade = sportsGrade;
    }

    public void putGrade() {
        System.out.println("Grade in sports: " + sportsGrade);
    }

    public void displayResult() {
        int totalMarks = getMarks() + sportsGrade;

        if (totalMarks >= 90) {
            System.out.println("Result: Distinction");
        } else if (totalMarks >= 60) {
            System.out.println("Result: First Division");
        } else if (totalMarks >= 40) {
            System.out.println("Result: Pass");
        } else {
            System.out.println("Result: Fail");
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Student student = new Student(101, 75);

        System.out.println("Student Roll No: " + student.getRollNumber());
        System.out.println("Student Marks: " + student.getMarks());

        student.putMarks(80);
        System.out.println("Updated Marks: " + student.getMarks());

        Result result = new Result(102, 85, 10);

        System.out.println("\nStudent Roll No: " + result.getRollNumber());
        System.out.println("Student Marks: " + result.getMarks());
        result.putGrade();

        result.displayResult();
    }
}