May 11, 2023

Brute Force Techniques I

This is a description

Student Class

import java.util.Scanner;

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

	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);
	}
}

public class StudentInput {
	public static void main(String[] args) {
		Student student = new Student();
		student.assign();
		student.compute();
		student.display();
	}
}

Employee Class

import java.util.Scanner;

class Employee {
	String name;
	String id;
	double basic = 0;
	double da = 0;
	double it = 0;
	double grossSalary = 0;
	double netSalary = 0;

	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 compute() {
		this.da = 0.52 * this.basic;
		this.grossSalary = this.basic + this.da;
		this.it = 0.3 * this.grossSalary;
		this.netSalary = this.grossSalary + this.it;

		System.out.println("Gross Salary, Net Salary and IT computed.");
	}

	void display() {
		System.out.println("Name: "+this.name);
		System.out.println("ID: "+this.id);
		System.out.println("DA: Rs. "+this.da);
		System.out.println("IT: Rs. "+this.it);
		System.out.println("Gross Salary: Rs. "+this.grossSalary);
		System.out.println("Net Salary: Rs. "+this.netSalary);
	}
}

public class EmployeeInput {
	public static void main(String[] args) {
		Employee employee = new Employee();
		employee.read();
		employee.compute();
		employee.display();
	}
}

Mixer

import java.util.Scanner;

public class Mixer {
	int arr[];
	int n;
	Scanner scanner = new Scanner(System.in);

	Mixer(int length) {
		this.n = length;
		this.arr = new int[length];
	}

	void accept() {
		System.out.println("Enter " + n + " elements in ascending order:");
		for (int i = 0; i < n; i++) {
			this.arr[i] = scanner.nextInt();
		}
	}

	Mixer mix(Mixer A) {
		Mixer B = new Mixer(n + A.n);
		int x = 0;
		for (int i = 0; i < n; i++) {
			B.arr[x++] = this.arr[i];
		}
		for (int j = 0; j < A.n; j++) {
			B.arr[x++] = A.arr[j];
		}
		return B;
	}

	void display() {
		System.out.println("The MIXED ARRAY is:");
		for (int i = 0; i < n; i++) {
			System.out.print(this.arr[i] + ", ");
		}
	}

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

		System.out.println("Enter size of 1ST ARRAY:");
		int sizeOfOne = scanner.nextInt();
		Mixer P = new Mixer(sizeOfOne);
		P.accept();

		System.out.println("Enter size of 2ND ARRAY:");
		int sizeOfTwo = scanner.nextInt();
		Mixer Q = new Mixer(sizeOfTwo);
		Q.accept();

		Mixer R = P.mix(Q);
		R.display();
		scanner.close();
	}
}

Stack Implementation

import java.util.Arrays;
import java.util.Scanner;

class Stack {
	int[] arr = new int[100];
	int top = 0;
	Scanner scanner = new Scanner(System.in);

	void push() {
		System.out.println("Enter the element: ");
		int element = scanner.nextInt();

		this.arr[top++] = element;
		System.out.println("\nElement pushed!\n");
	}

	void pop() {
		this.arr[--top] = 0;
		System.out.println("\nElement popped!\n");
	}

	void display() {
		System.out.print("Elements are: \n[ ");
		for (int i = 0; i < top; i++) {
			System.out.print(this.arr[i] + ", ");
		}
		System.out.print("]\nTop is: " + top + "\n\n");
	}
}

public class StackInput {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		Stack stack = new Stack();
		String choice = null;

		System.out.println("Enter number of elements: ");
		int num = scanner.nextInt();

		System.out.println("Enter the elements: ");
		for (int i = 0; i < num; i++) {
			stack.arr[i] = scanner.nextInt();
		}
		stack.top = num;

		do {
			System.out.print("--- STACK OPERATIONS ---");
			System.out.print("\n1. Push Element");
			System.out.print("\n2. Pop Element");
			System.out.print("\n3. Display Elements");
			System.out.print("\n4. Exit.");
			System.out.print("\n\nYour input: ");
			int input = scanner.nextInt();

			switch (input) {
				case 1:
					stack.push();
					break;
				case 2:
					stack.pop();
					break;
				case 3:
					stack.display();
					break;
				case 4:
					System.exit(0);
				default:
					System.out.println("Invalid input.");
			}

			System.out.println("Do you want to continue? [y/N]");
			choice = scanner.next();
		} while (choice.equals("y"));

		scanner.close();
	}
}