java_2LabCycle
java_2LabCycle
Date: 01-05-2024
Aim: Define an interface called Shape with methods calculateArea() and calculatePerimeter().
Create classes Circle, Rectangle, and Triangle that implement the Shape interface and provide
implementations for these methods.
Program:
import java.util.*;
interface shape {
void Area(int v1, int v2);
void Perimeter(int v1, int v2, int v3);
}
class circle implements shape {
int r;
public void Area(int r, int x) {
System.out.println("\nArea of circle: " + 3.14 * r * r);
}
public void Perimeter(int r, int x, int x1) {
System.out.println("\nPerimeter of circle: " + 2 * 3.14 * r);
}
}
class rectangle implements shape {
int l, b1;
public void Area(int l, int b1) {
System.out.println("\nArea of rectangle: " + l * b1);
}
public void Perimeter(int l, int b, int x) {
System.out.println("\nPerimeter of rectangle: " + 2 * (l + b1));
}
}
class triangle implements shape {
int h, b2, s1, s2, s3;
public void Area(int b2, int h) {
System.out.println("\nArea of triangle: " + 0.5 * b2 * h);
}
public void Perimeter(int s1, int s2, int s3) {
System.out.println("\nPerimeter of triangle: " + (s1 + s2 + s3));
}
}
class Shapes {
public static void main(String args[]) {
int r;
int l, b1, s1, s2, s3, b2, h;
System.out.println("Enter the radius of circle: ");
Scanner sc = new Scanner(System.in);
r = sc.nextInt();
System.out.println("Enter the length of rectangle: ");
l = sc.nextInt();
System.out.println("Enter the breadth of rectangle: ");
b1 = sc.nextInt();
System.out.println("Enter the base of triangle: ");
b2 = sc.nextInt();
System.out.println("Enter the height of triangle: ");
h = sc.nextInt();
System.out.println("Enter first side of the triangle: ");
s1 = sc.nextInt();
System.out.println("Enter second side of the triangle: ");
s2 = sc.nextInt();
System.out.println("Enter third side of the triangle: ");
s3 = sc.nextInt();
shape obj = new circle();
obj.Area(r, 0);
obj.Perimeter(r, 0, 0);
shape obj1 = new rectangle();
obj1.Area(l, b1);
obj1.Perimeter(l, b1, 0);
shape obj2 = new triangle();
obj2.Area(b2, h);
obj2.Perimeter(s1, s2, s3);
}
}
Output:
product.setProductPrice(sc.nextFloat());
stock[i].setProduct(product);
stock[i].setProductQuantity(sc.nextInt());
}}
public void displayProducts() {
System.out.println("+----------+----------+----------- +");
System.out.printf("| %-8s | %-8s | %-8s |\n", "Name", "Price", "Quantity");
System.out.println("+----------+----------+-----------+");
for (int i = 0; i < n; i++) {
System.out.printf("| %-8s | %-8.2f | %-8d |\n", stock[i].getProduct().getProductName(),
stock[i].getProduct().getProductPrice(), stock[i].getProductQuantity());}
System.out.println("+----------+----------+----------- +");}
public void updateProducts()
{ String id;
int quan;
Scanner s = new Scanner(System.in);
System.out.print("Enter the product ID to update the stock:");
id = s.nextLine();
System.out.print("Enter the new Quantity:"); quan
= s.nextInt();
for (int i = 0; i < n; i++) {
if (stock[i].getProduct().getProductID().equals(id))
{ stock[i].setProductQuantity(quan);
break;}}
displayProducts();}
public void generateReport()
{ System.out.println("Stock Report!!");
System.out.println("+----------+---------- +");
System.out.printf("| %-8s | %-8s |\n", "Name", "Quantity");
System.out.println("+----------+---------- +");
for (int i = 0; i < n; i++) {
System.out.printf("| %-8s | %-8d |\n", stock[i].getProduct().getProductName(),
stock[i].getProductQuantity());}
System.out.println("+----------+---------- +");
}
}
InventorySystem.java
import inventory.Inventory;
public class InventorySystem {
public static void main(String[] args)
{ Inventory inventory = new Inventory();
inventory.addProduct();
inventory.displayProducts();
inventory.updateProducts();
inventory.generateReport();
}
}
Output:
Enter the number of products to be entered:2
Enter the details of the product 1
Enter product ID:1001 Enter
product name:laptop Enter
product price:10000 Enter
product quantity:5
Enter the details of the product 2
Enter product ID:1002
Enter product name:phone
Enter product price:25000
Enter product quantity:10
Enter the product ID to update the stock:1001
Enter the new Quantity:10
+ + + +
| Name | Price | Quantity |
+ + + +
| laptop | 10000.00 | 5 |
| phone | 25000.00 | 10 |
| Name | Price | Quantity |
+ + + +
| laptop | 10000.00 | 10 |
| phone | 25000.00 | 10 |
+ + + +
Stock Report!!
+ + +
| Name | Quantity |
+ + +
| laptop | 10 |
| phone | 10 |
+ + +
Program No: 13
Date: 26-04-2024
Aim: Write a Java program that takes two integers as input from the user and performs division.
Implement exception handling to catch and handle the Arithmetic Exception that may occur if the
user tries to divide by zero.
Program:
import java.util.Scanner;
public class Division {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a, b;
float result = 0;
System.out.println("Enter two numbers to divide");
a = sc.nextInt();
b = sc.nextInt(); try
{
result = a / b;
} catch (Exception e) {
System.out.println("Exception:" + e);
}
System.out.println("Result: " + result);
}
}
Output:
Enter two numbers to divide
20
Exception:java.lang.ArithmeticException: / by zero
Result: 0.0
Program No: 14
Date: 26-04-2024
Aim: Write a Java program that initializes an array with a fixed size and tries to access an element
at an index outside the array's bounds. Implement exception handling to catch and handle
ArrayIndexOutOfBoundsException.
Program:
import java.util.Scanner; public
class ArrayControl {
public static void main(String[] args) {
int a[]=new int[5];
Scanner sc =new Scanner(System.in);
System.out.println("Enter 5 elements of the array");
for(int i=0;i<5;i++){
a[i]=sc.nextInt();} int
sum = 0;
try {
for (int i = 0; i < 10; i++)
{ System.out.println(i);
sum += a[i];}
} catch (Exception e)
{ System.out.println("Exception:" + e);}
System.out.println("Sum of the elements of the array:" + sum);}}
Output:
Enter 5 elements of the array
34567
0
1
2
3
4
5
Exception:java.lang.ArrayIndexOutOfBoundsException: 5
Sum of the elements of the array:25
Program No: 15
Date: 28-04-2024
Aim: Create a Java program for a bookstore that sells books based on the buyer's age. Implement a
user-defined exception called AgeRestrictionException, which should be thrown if a buyer's age
is below 18 years, as the store only sells books to individuals who are 18 years or older.
Handle this exception appropriately in your program.
Program:
import java.util.Scanner;
class Book {
String title;
double price;
public Book(String title, double price) {
this.title = title;
this.price = price;
}
public String getTitle() {
return title;
}
public double getPrice() {
return price;
}
}
class Books {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter details for Book 1:");
System.out.print("Title: ");
String title1 = sc.nextLine();
System.out.print("Price: ");
double price1 = sc.nextDouble();
sc.nextLine();
System.out.println("Enter details for Book 2:");
System.out.print("Title: ");
String title2 = sc.nextLine();
System.out.print("Price: ");
double price2 = sc.nextDouble();
sc.nextLine();
System.out.println("Enter details for Book 3:");
System.out.print("Title: ");
String title3 = sc.nextLine();
System.out.print("Price: ");
double price3 = sc.nextDouble();
Book b1 = new Book(title1, price1);
Book b2 = new Book(title2, price2);
Book b3 = new Book(title3, price3);
Book[] books = { b1, b2, b3 };
System.out.println("List of Books\n");
System.out.println("1. " + b1.getTitle());
System.out.println("2. " + b2.getTitle());
System.out.println("3. " + b3.getTitle());
System.out.println("Select your choice: ");
int choice = sc.nextInt();
choice--;
try
{
System.out.print("Please enter your age: ");
int age = sc.nextInt();
if (age < 18)
{
throw new Exception(books[choice].getTitle() + " cannot be sold to a buyer younger than 18
years old.");
}
System.out.println("Sold book: " + books[choice].getTitle() + " for Rupees:" +
books[choice].getPrice());
}
catch (Exception e)
{
System.out.println("Error: " + e.getMessage());
}
}
}
Output:
Enter details for Book 1:
Title: Wings of fire
Price: 350
Enter details for Book 2:
Title: C programming
Price: 590
Enter details for Book 3:
Title: The Hobbit
Price: 250
List of Books
1. Wings of fire
2. C programming
3. The Hobbit
Select your choice: 2
Please enter your age: 22
Sold book: C programming for Rupees:590.0
1. Wings of fire
2. C programming
3. The Hobbit
Select your choice: 1
Please enter your age: 16
Error: Wings of fire cannot be sold to a buyer younger than 18 years old.
Program No: 16
Date: 05-05-2024
Aim: Write a Java program that demonstrates the use of threads to perform concurrent tasks.
Create two threads, one to print even numbers from 1 to 10 and another to print odd numbers
from 1 to 10.
Program:
class EvenThread extends Thread {
public void run() {
for (int i = 2; i <= 10; i += 2) {
System.out.println(i);
} }}
class OddThread extends Thread {
public void run() {
for (int i = 1; i <= 10; i += 2) {
System.out.println(i);
}}}
public class ThreadMain {
public static void main(String[] args) {
EvenThread even = new EvenThread();
OddThread odd = new OddThread();
even.start();
odd.start();
}}
Output
1
3
5
7
9
2
4
6
8
10
Program No: 17
Date: 06-05-2024
Aim: Implement the above program with synchronization.
Program:
class EvenOdd {
synchronized public static void printEven() {
for (int i = 2; i <= 10; i += 2) {
System.out.println(i);
} }
synchronized public static void printOdd() {
for (int i = 1; i <= 10; i += 2) {
System.out.println(i);
} }}
class EvenThread extends Thread {
public void run() {
EvenOdd.printEven();
} }
class OddThread extends Thread {
public void run() {
EvenOdd.printOdd();
}
}
public class ThreadSync {
public static void main(String[] args) {
EvenThread even = new EvenThread();
OddThread odd = new OddThread();
even.start();
odd.start();
}}
Output:
2
4
6
8
10
1
3
5
7
9