May 11, 2023

Brute Force Techniques I

This is a description

Student Subclasses

import java.util.*;

class Student {
	String name;
	int[] marks = new int[5];
	int total, average;

	Student() {
		this.assign();
		this.compute();
	}

	void assign() {
		Scanner scanner = new Scanner(System.in);

		System.out.println("Enter student name: ");
		this.name = scanner.nextLine();
		System.out.println("Enter marks in 5 subjects: ");
		for (int i = 0; i < 5; i++) {
			this.marks[i] = scanner.nextInt();
		}
	}

	void compute() {
		this.total = 0;
		for (int i = 0; i < this.marks.length; i++) {
			this.total += this.marks[i];
		}
		this.average = this.total / this.marks.length;

		System.out.println("Total and average marks computed.");
	}

	void display() {
		System.out.println("Name: " + this.name);
		System.out.println("Total Marks: " + this.total);
		System.out.println("Average Marks: " + this.average);
	}
}

class ScienceStudent extends Student {
	private int practicalMarks;

	ScienceStudent(int practicalMarks) {
		super();
		this.practicalMarks = practicalMarks;
		this.compute();
	}

	int displayPracticalMarks() {
		return this.practicalMarks;
	}

	void compute() {
		super.compute();
		this.total += this.practicalMarks;
		this.average = (this.average + this.practicalMarks) / 2;
	}
}

class ArtsStudent extends Student {
	private int electiveMarks;

	ArtsStudent(int electiveMarks) {
		super();
		this.electiveMarks = electiveMarks;
		this.compute();
	}

	int displayElectiveMarks() {
		return this.electiveMarks;
	}

	void compute() {
		super.compute();
		this.total += this.electiveMarks;
		this.average = (this.average + this.electiveMarks) / 2;
	}
}

public class StudentSubclasses {
	public static void main(String[] args) {
		System.out.println("--- STUDENT ---");
		Student student = new Student();
		student.display();
		System.out.println("--- SCIENCE STUDENT ---");
		ScienceStudent scienceStudent = new ScienceStudent(90);
		scienceStudent.display();
		System.out.println("--- ARTS STUDENT ---");
		ArtsStudent artsStudent = new ArtsStudent(90);
		artsStudent.display();
	}
}
PS F:\oopsies>
--- STUDENT ---
Enter student name:
Abhigyan Trips
Enter marks in 5 subjects:
90
91
92
93
94
Total and average marks computed.
Name: Abhigyan Trips
Total Marks: 460
Average Marks: 92
--- SCIENCE STUDENT ---
Enter student name:
Tripathi
Enter marks in 5 subjects:
95
94
93
92
91
Total and average marks computed.
Total and average marks computed.
Name: Tripathi
Total Marks: 555
Average Marks: 91
--- ARTS STUDENT ---
Enter student name:
Yogesh
Enter marks in 5 subjects:
89
84
99
99
99
Total and average marks computed.
Total and average marks computed.
Name: Yogesh
Total Marks: 560
Average Marks: 92

Employee Subclasses

import java.util.*;

class Employee {
	String name;
	String id;
	double basic = 0;

	Employee() {
		this.read();
	}

	void read() {
		Scanner scanner = new Scanner(System.in);

		System.out.println("Enter employee name: ");
		this.name = scanner.nextLine();
		System.out.println("Enter employee ID: ");
		this.id = scanner.nextLine();
		System.out.println("Enter basic salary: ");
		this.basic = scanner.nextInt();
	}

	void display() {
		System.out.println("Name: " + this.name);
		System.out.println("ID: " + this.id);
		System.out.println("Net Salary: Rs. " + this.basic);
	}
}

class PartTimeEmployee extends Employee {
	int hoursWorked;
	static final double hourlyRate = 1200;

	PartTimeEmployee(Scanner scanner) {
		System.out.println("Enter hours worked:");
		this.hoursWorked = scanner.nextInt();
		this.compute();
	}

	void compute() {
		this.basic = hourlyRate * hoursWorked;
	}

	void display() {
		super.display();
		System.out.println("Hours Worked: " + this.hoursWorked);
		System.out.println("Hourly Rate: Rs. " + PartTimeEmployee.hourlyRate);
	}
}

class FullTimeEmployee extends Employee {
	double bonus, deductions;

	FullTimeEmployee(Scanner scanner) {
		System.out.println("Enter bonus:");
		this.bonus = scanner.nextDouble();
		System.out.println("Enter deductions: ");
		this.deductions = scanner.nextDouble();
	}

	void display() {
		super.display();
		System.out.println("Bonus: Rs. " + this.bonus);
		System.out.println("Deductions: Rs. " + this.deductions);
	}
}

public class EmployeeSubclasses {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);

		System.out.println("--- EMPLOYEE ---");
		Employee employee = new Employee();
		employee.display();
		System.out.println("--- PART-TIME EMPLOYEE ---");
		PartTimeEmployee partTimeEmp = new PartTimeEmployee(scanner);
		partTimeEmp.display();
		System.out.println("--- FULL-TIME EMPLOYEE ---");
		FullTimeEmployee fullTimeEmp = new FullTimeEmployee(scanner);
		fullTimeEmp.display();
	}
}

Person, College & Graduate

class Person {
	private String name;
	private String dateOfBirth;

	Person(String name, String dateOfBirth) {
		this.name = name;
		this.dateOfBirth = dateOfBirth;
	}

	String getName() {
		return this.name;
	}

	String getDateOfBirth() {
		return this.dateOfBirth;
	}
}

class College extends Person {
	private double cgpa;

	College(String name, String dateOfBirth, double cgpa) {
		super(name, dateOfBirth);
		this.cgpa = cgpa;
	}

	double getCgpa() {
		return this.cgpa;
	}
}

class Graduate extends College {
	private String dateOfGrad;

	Graduate(String name, String dateOfBirth, double cgpa, String dateofGrad) {
		super(name, dateOfBirth, cgpa);
		this.dateOfGrad = dateofGrad;
	}

	String getDateOfGrad() {
		return this.dateOfGrad;
	}

	void display() {
		System.out.println("Name: " + this.getName());
		System.out.println("Date of Birth: " + this.getDateOfBirth());
		System.out.println("CGPA: " + this.getCgpa());
		System.out.println("Date of Graduation: " + this.getDateOfGrad());
	}
}

public class Main {
	public static void main(String[] args) {
		Graduate grad = new Graduate("Abhigyan", "15/11/2004", 8.52, "22/07/2026");
		grad.display();
	}
}

Building, House & School

class Building {
	private int squareFootage;
	private int stories;

	Building(int squareFootage, int stories) {
		this.squareFootage = squareFootage;
		this.stories = stories;
	}
}

class House extends Building {
	private int bhk;

	House(int squareFootage, int stories, int bhk) {
		super(squareFootage, stories);
		this.bhk = bhk;
	}
}

class School extends Building {
	private int classes;
	private String level;

	School(int squareFootage, int stories, int classes, String level) {
		super(squareFootage, stories);
		this.classes = classes;
		this.level = level;
	}
}

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