JAVA programslab
JAVA programslab
1. A) Prime numbers
class PrimeNumbers {
public static void main(String args[]) {
int prime;
int n1 = Integer.parseInt(args[0]);
int n2 = Integer.parseInt(args[1]);
System.out.println("Prime numbers in the given range are:");
for (int i = n1; i <= n2; i++) {
prime = 1;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
prime = 0;
break;
}
}
if (prime == 1)
System.out.println(i + " ");
}
}
}
b)Quadratic equation
class QuadraticEquation {
public static void main(String[] args) {
double a = Double.parseDouble(args[0]);
double b = Double.parseDouble(args[1]);
double c = Double.parseDouble(args[2]);
double disc = b * b - 4 * a * c;
double sqrt = Math.sqrt(disc);
if (disc > 0) {
double root1 = (-b + sqrt) / (2 * a);
double root2 = (-b - sqrt) / (2 * a);
System.out.println("Roots are: " + root1 + " and " + root2);
} else if (disc == 0) {
double root = -b / (2 * a);
System.out.println("Root is: " + root);
} else {
System.out.println("Roots are imaginary.");
}
}
}
C)Pyramid
class Pyramid {
public static void main(String args[]) {
int n = Integer.parseInt(args[0]);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*\t");
System.out.println();
}
}
}
2)Bank account
import java.util.*;
class Account {
private int AccNo;
private String name;
private double balance;
private String AccType;
Scanner sc = new Scanner(System.in);
Account() {
AccNo = 0;
name = "";
balance = 0.0;
AccType = "";
}
void getDetails() {
System.out.print("Enter account holder's name: ");
name = sc.next();
System.out.print("Enter Account Number: ");
AccNo = sc.nextInt();
System.out.print("Enter the balance amount: ");
balance = sc.nextDouble();
System.out.print("Enter account type: ");
AccType = sc.next();
}
void printDetails() {
System.out.println("Acc No\tName\tBalance amount\tType of account");
System.out.println(AccNo + "\t\t" + name + "\t\t" + balance + "\t\t" + AccType);
}
void deposit() {
double amt;
System.out.print("\nEnter the amount to be deposited: ");
amt = sc.nextDouble();
balance += amt;
System.out.print("\nAmount deposited successfully....\nCurrent Balance: " + balance);
}
void withdrawal() {
double amt;
System.out.print("\nEnter the amount to be withdrawn: ");
amt = sc.nextDouble();
if (balance >= amt) {
balance -= amt;
System.out.print("Transaction successful......\nCurrent Balance: " + balance);
} else {
System.out.print("\nInsufficient balance in account....\nTransaction failed.....");
}
}
int choice;
do {
System.out.println(
"\n\n********* Main Menu **********\n\nEnter\n1. To display all details of account
holder");
System.out.println("2. Search details by account number\n3. To deposit amount\n4. To withdraw the
amount");
System.out.println("5. To exit from the program\n");
switch (choice) {
case 1:
for (int i = 0; i < n; i++)
holder[i].printDetails();
break;
case 2:
System.out.print("\nEnter the account number: ");
int acn = sc.nextInt();
boolean found = false;
for (int i = 0; i < n; i++) {
found = holder[i].search(acn);
if (found)
break;
}
if (!found) {
System.out.print("\nSearch failed.....\nAccount doesn't exists");
}
break;
case 3:
System.out.print("\nEnter the account number: ");
int ac = sc.nextInt();
boolean dep = false;
for (int i = 0; i < n; i++) {
dep = holder[i].search(ac);
if (dep) {
holder[i].deposit();
break;
}
}
if (!dep) {
System.out.print("\nSearch failed....\nAccount doesn't exits");
}
break;
case 4:
System.out.print("\nEnter account number: ");
int acc = sc.nextInt();
boolean fou = false;
for (int i = 0; i < n; i++) {
fou = holder[i].search(acc);
if (fou) {
holder[i].withdrawal();
break;
}
}
if (!fou) {
System.out.print("\nSearch failed.....\nAccount doesn't exists");
}
break;
case 5:
System.out.print("Thank you.....");
break;
}
class Temp {
static int i, j;
System.out.println("City with highest temperature is " + maxCity + " with " + maxTemp + " Celsius.");
System.out.println("City with lowest temperature is " + minCity + " with " + minTemp + " Celsius.");
}
}
4)ELECTION
import java.util.*;
class Vote {
Scanner s = new Scanner(System.in);
int voter_id;
static int count[] = new int[6];
int ballot;
void read() {
System.out.print("Enter the voter ID: ");
voter_id = s.nextInt();
void calculateVotes() {
switch (ballot) {
case 1: count[1] += 1; break;
case 2: count[2] += 1; break;
case 3: count[3] += 1; break;
case 4: count[4] += 1; break;
case 5: count[5] += 1; break;
default: count[0] += 1; break;
}
}
class Election {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of voters: ");
int n = sc.nextInt();
5)ARRAY
import java.util.Arrays;
import java.util.Scanner;
class MergeArray {
int[] array1;
int[] array2;
MergeArray()
{}
MergeArray(int[] a, int[] b) {
array1 = a;
array2 = b;
}
6) A)CUBE
import java.util.Scanner;
class Cube {
float a;
void read(Scanner scanner) {
System.out.print("Enter the value of the side of the cube: ");
a = scanner.nextFloat();
}
void volume() {
float v = a * a * a;
System.out.println("The volume is: " + v);
}
void area() {
float area = 6 * a * a;
System.out.println("The area is: " + area);
}
}
7)Student SCORE
import java.util.*;
class Student {
int rollNumber;
void readNumber(int n) {
rollNumber = n;
}
void printNumber() {
System.out.println("Roll No: " + rollNumber);
}
}
void printMarks() {
System.out.println("Marks in Subject 1: " + sub1);
System.out.println("Marks in Subject 2: " + sub2);
System.out.println("Marks in Subject 3: " + sub3);
}
}
Void display(){
total = sub1+sub2+sub3;
PrintNumber();
PrintMarks();
System.out.println(“Total score:”+total);
}
}
System.out.println("Highest marks in Subject 1: " + highSub1 + " (Roll No: " + highRollSub1 + ")");
System.out.println("Highest marks in Subject 2: " + highSub2 + " (Roll No: " + highRollSub2 + ")");
System.out.println("Highest marks in Subject 3: " + highSub3 + " (Roll No: " + highRollSub3 + ")");
System.out.println("Highest total marks: " + highTotal + " (Roll No: " + highRollTotal + ")");
sc.close();
}
}
8)BOOK DETAILS
import java.util.Scanner;
class Book {
Scanner sc = new Scanner(System.in);
String name;
int code, quantity;
double unitPrice, totalPrice;
void inputData() {
System.out.print("Enter the name of the Book: ");
name = sc.nextLine();
System.out.print("Enter the code of Book: ");
code = sc.nextInt();
System.out.print("Enter the Price of Book: ");
unitPrice = sc.nextDouble();
System.out.print("Enter the number of Books: ");
quantity = sc.nextInt();
void outputData() {
System.out.println(name + "\t" + code + "\t" + unitPrice + "\t\t" + quantity + "\t\t" + totalPrice);
}
}
System.out.println("\nName\tCode\tUnitPrice\tQuantity\tTotalPrice");
for (int i = 0; i < u; i++)
books[i].outputData();
sc.close();
}
}
9)EMPLOYEE DETAILS
import java.util.Scanner;
class Employee {
Scanner sc = new Scanner(System.in);
private String ID;
private String name;
private String department;
private double basicPay;
private double grossPay;
private double netPay;
private double totalTax;
void readDetails() {
System.out.print("Enter employee-ID: ");
ID = sc.nextLine();
System.out.print("Enter name: ");
name = sc.nextLine();
System.out.print("Enter department: ");
department = sc.nextLine();
System.out.print("Enter basic pay: ");
basicPay = sc.nextDouble();
sc.close();
grossPay = computeGrossPay();
netPay = computeNetPay();
}
double computeGrossPay() {
double DA = (0.58 * basicPay);
double HRA = (0.16 * basicPay);
return (basicPay + DA + HRA);
}
double computeNetPay() {
if (grossPay <= 200000) {
totalTax = 0;
} else if (grossPay <= 300000) {
totalTax = (0.10 * grossPay);
} else if (grossPay <= 500000) {
totalTax = (.15 * grossPay);
} else {
double tax = (.30 * grossPay);
totalTax = (tax + (0.02 * tax));
}
return (grossPay - totalTax);
}
void displayDetails() {
System.out.println(name + "\t" + ID + "\t" + department + "\t" + basicPay + "\t" + totalTax + "\t" + netPay);
}
}
10)BANK DETAILS
import java.util.*;
abstract class Bank {
char bankType;
double amount, interest;
boolean platinumCard;
boolean offerCreditCard;
void getRoi() {
if (bankType == 'n') {
platinumCard = false;
if (term >= 3)
interest = 0.07 * termDeposit*term;
} else {
platinumCard = true;
if (term >= 3)
interest = 0.08 * termDeposit*term;
}
}
@Override
void deposit() {
System.out.println("Enter the type of bank\n'n' - for Nationalised Bank \n'i' - for International Bank ");
bankType = sc.next().charAt(0);
if (bankType == 'n' || bankType == 'i') {
System.out.print("Enter the amount to be deposited in the bank: ");
termDeposit = sc.nextDouble();
System.out.print("Enter the term for which amount has been deposited (in years): ");
term = sc.nextInt();
getRoi();
} else
System.out.println("Invalid Bank type!");
}
void display() {
String bank;
if (bankType == 'n')
bank = "Nationalised Bank";
else if (bankType == 'i')
bank = "International Bank";
else
bank = "Invalid Bank type";
class Student {
int rollNumber;
void readNumber(int n) {
rollNumber = n;
}
void printNumber() {
System.out.println("Roll No: " + rollNumber);
}
}
void printMarks() {
System.out.println("Marks in Subject-1: " + sub1);
System.out.println("Marks in Subject-2: " + sub2);
}
}
interface Sports {
void readWt(double n);
void printWt();
}
void display() {
total = sub1 + sub2 + sportWt;
printNumber();
printMarks();
printWt();
System.out.println("Total Score: " + total);
}
}
12)STACK IMPLEMENTATION
import java.util.*;
class StackException extends Exception {
StackException(String str) {
super(str);
}
}
class Stack {
private final int[] stk;
private int tos;
Stack(int size) {
stk = new int[size];
tos = -1;
}
void display() {
if (tos > -1) {
System.out.print("Stack elements are: ");
for (int i = tos; i > -1; i--)
System.out.print(stk[i] + " ");
} else
System.out.println("Stack Underflow!\n");
}
void peek() {
if (tos == -1)
System.out.println("Stack Underflow!\n");
else
System.out.println("Peek element : " + stk[tos]);
}
}
do {
System.out.println("\nEnter~\n1. To push\n2. To pop\n3. To get peek element\n4. To display\n0. To Exit");
System.out.print("Enter your Choice: ");
ch = sc.nextInt();
switch (ch) {
case 1:
try {
System.out.print("Enter the element: ");
stkObj.push(sc.nextInt());
} catch (StackException e) {
System.err.println("Exception Caught: " + e);
}
break;
case 2:
try {
System.out.println("Popped element: " + stkObj.pop());
} catch (StackException e) {
System.err.println("Exception Caught: " + e);
}
break;
case 3:
stkObj.peek();
break;
case 4:
stkObj.display();
break;
case 0:
break;
default:
System.out.println("Invalid Choice!");
}
} while (ch != 0);
}
}