Java Programming: Lab Assessment - 5
Java Programming: Lab Assessment - 5
LAB ASSESSMENT – 5
Name: Mounika
Reg No.: 20BCE2963
Slot: L31+L32
Faculty: Prof. Jabanjalin Hilda J
Date: 18/04/2022
ACTIVITY – 7
Q1)
Code:
package act7q1;
class OddThread extends Thread{
int limit;
sharedPrinter printer;
public OddThread(int limit,sharedPrinter printer){
this.limit=limit;
this.printer=printer;
}
public void run(){
int oddnumber=1;
while(oddnumber<=limit){
printer.printOdd(oddnumber);
oddnumber=oddnumber+2;
}
}
}
class EvenThread extends Thread{
int limit;
sharedPrinter printer;
public EvenThread(int limit,sharedPrinter printer){
this.limit=limit;
this.printer=printer;
}
public void run(){
int evenNumber=2;
while(evenNumber<=limit){
printer.printEven(evenNumber);
evenNumber =evenNumber+2;
}
}
}
class sharedPrinter{
boolean isOddPrinted = false;
synchronized void printOdd(int number){
while(isOddPrinted){
try{
wait();
}
catch(InterruptedException e){
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+":"+number);
isOddPrinted=true;
try{
Thread.sleep(1000);
}
catch(InterruptedException e){
e.printStackTrace();
}
notify();
}
synchronized void printEven(int number){
while(!isOddPrinted)
{
try{
wait();
}
catch (InterruptedException e){
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+":"+number);
isOddPrinted=false;
try{
Thread.sleep(1000);
}
catch(InterruptedException e){
e.printStackTrace();
}
notify();
}
}
public class Act7q1 {
public static void main(String args[]){
sharedPrinter printer = new sharedPrinter();
OddThread oddThread = new OddThread(10, printer);
oddThread.setName("Odd-Thread");
EvenThread evenThread = new EvenThread(10, printer);
evenThread.setName("Even-Thread");
oddThread.start();
evenThread.start();
System.out.println("Mounika 20BCE2963");
}
}
Output:
ACTIVITY – 8
Q1)
Code:
package act8q1;
class items{
int array[] = {15,5,6,30,10};
String array2[] = {"Pen","Pencil","Eraser","Sketch","Paper"};
int temp=-1;
synchronized void delete(String n) throws InterruptedException{
for (int i = 0; i < 5; i++) {
if(array2[i].compareTo(n)==0){
temp=i;
}
}
System.out.println(array2[temp]+" Deleted.");
array2[temp]="";
notify();
}
synchronized void delete2() throws InterruptedException{
if(temp==-1)
try {
wait();
} catch (Exception e) {
}
array[temp]=-1;
notify();
}
synchronized void disp(String n) throws InterruptedException{
for (int i = 0; i < 5; i++) {
if(array2[i].compareTo(n)==0){
temp=i;
}
}
System.out.println(array2[temp]+" "+array[temp]);
}
}
public class Act8q1 {
public static void main(String[] args) {
System.out.println("20BCE2963 Mounika\n");
items t = new items();
Thread t1 = new Thread(new Runnable(){
public void run() {
try {
t.delete("Pen");
} catch (InterruptedException ex) {}
}
});
Thread t2 = new Thread(new Runnable(){
public void run() {
try {
t.delete2();
} catch (InterruptedException ex) {}
}
});
Thread t3 = new Thread(new Runnable(){
public void run() {
try {
t.disp("Paper");
} catch (InterruptedException ex) {}
}
});
t1.start();
t2.start();
t3.start();
}
}
Output:
Q2)
Code:
package act8q2;
class registration
{
String bus_name;
public int seats;
registration(String a,int b)
{
bus_name=a;
seats=b;
}
public synchronized void registr_seat() throws InterruptedException
{
if (seats==0)
{
System.out.println("waiting...");
wait();
}
System.out.println("registering the seat");
seats=seats-1;
}
public synchronized void allot_seats(int m)
{
System.out.println("Alloting 60 seats");
this.seats=this.seats+m;
System.out.println("Notifying seats");
notify();
}
void print_seats()
{
System.out.println("Remaing Seats : " +seats);
}
}
public class Act8Q2 {
public static void main(String[] args) throws InterruptedException {
System.out.println("20BCE2963 Mounika\n");
registration r= new registration("red bus",0);
Thread t1 = new Thread(()->{
try {
r.registr_seat();
} catch (InterruptedException ex) {
}
});
Thread t2 = new Thread(()->{
try {
r.registr_seat();
} catch (InterruptedException ex) {
}
});
Thread t3 = new Thread(()->{
r.allot_seats(60);
});
Thread t4 = new Thread(()->{
r.print_seats();
});
t1.start();
t2.start();
t3.start();
t1.join();
t2.join();
t3.join();
t4.start();
t4.join();
}
}
Output:
Q3)
Code:
package act8q31;
import java.util.LinkedList;
public class BakeryShop {
LinkedList<Integer> list = new LinkedList<>();
int capacity = 10;
public void produce() throws InterruptedException {
int value = 0;
while (true) {
synchronized (this) {
while (list.size() == capacity) {
wait();
}
System.out.println("Cake Baked:" + value);
list.add(value++);
notify();
Thread.sleep(1000);
}
}
}
public void consume() throws InterruptedException {
while (true) {
synchronized (this) {
while (list.isEmpty())
wait();
}
int val = list.removeFirst();
System.out.println("Customer Bought Cake:" + val);
notify();
Thread.sleep(1000);
}
}
}
class Main {
public static void main(String[] args) {
System.out.println("20BCE2963");
final BakeryShop bs = new BakeryShop();
Thread t1 = new Thread(new Runnable() {
public void run() {
try {
bs.produce();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
bs.consume();
} catch (InterruptedException e) {
}
}
});
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Output:
Activity – 9
Files:
Q1)
Code:
package act9q11;
import java.io.*;
public class Act9q11 {
public static void main(String[] args) {
System.out.println("20BCE2963");
try {
FileOutputStream fos=new FileOutputStream("Test.txt");
String s="Mounika Varanasi";
byte b[]=s.getBytes();
fos.write(b);
fos.close();
System.out.println("Success!");
}catch(Exception e) {System.out.println(e);}
try {
FileInputStream fis=new FileInputStream("Test.txt");
StringBuffer sb=new StringBuffer();
int i=0;
while((i=fis.read())!=-1){
sb.append((char)i);
}
sb.reverse();
System.out.println("Reversed content: "+sb);
fis.close();
}catch(Exception e) {System.out.println(e);}
}
}
Output:
Q2)
Code:
package act9q12;
import java.io.*;
public class Act9q12 {
public static void main(String[] args) {
System.out.println("20BCE2963");
try {
FileWriter fw=new FileWriter("Test.txt");
String s="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
fw.write(s);
fw.close();
System.out.println("English alphabets(A-Z) written to file");
}catch(Exception e) {System.out.println(e);}
try {
FileReader fr=new FileReader("Test.txt");
int i;
System.out.print("Reading file: ");
while((i=fr.read())!=-1){
System.out.print((char)i);
}
fr.close();
}catch(Exception e) {System.out.println(e);}
}
}
}
}
Output:
Q2)
Code:
package act9q2;
import java.io.*;
class StopWord{
String file1="D:\\jv prac\\class question\\src\\file1.txt";
String file2="D:\\jv prac\\class question\\src\\file2.txt";
void allstop(String t) {
try(BufferedReader b=new BufferedReader(new
FileReader(file1))){
String s;
int count=0;
while((s=b.readLine())!=null) {
String words[] = s.split(" ");
//System.out.println(s); //to print the lines
for(String i:words) {
//System.out.println(i); //print the words
if(i.equals("a") || i.equals("and") ||
i.equals("the")) {
count+=1;
}
}
}
System.out.println("[+] There are ["+count+"] stop words
{'a','and','the'} in the file. says "+t);
}
catch(FileNotFoundException e) { //Exception
System.out.println(e);
}
catch(IOException e) {
System.out.println(e);
}
}
void allexceptstop(String t){
try(BufferedReader b=new BufferedReader(new
FileReader(file2))){
String s;
int counth=0;
int count=0;
while((s=b.readLine())!=null) {
String words[] = s.split(" "); x
for(String i:words)
if(i.equals("a") || i.equals("and") ||
i.equals("the")) {
}
else if(i.startsWith("h") || i.startsWith("H")) {
counth+=1;
count+=1;
}
else{
count+=1;
}
}
}
System.out.println("[+] There are ["+count+"] words except
stop words {'a','and','the'} in the file, with ["+counth+"] words starting with
'h/H'. says "+t);
}
catch(FileNotFoundException e) { //Exception
System.out.println(e);
e.printStackTrace();
}
catch(IOException e) {
System.out.println(e);
e.printStackTrace();
}
}
void thankyou(String t) {
System.out.println("[+] Thankyou for using the service. says"+t);
}
}
public class Act9q2 {
public static void main(String[] args) throws InterruptedException {
System.out.println("*************** 20BCE2963 - Mounika
****************");
StopWord sw=new StopWord();
Thread t1;
t1 = new Thread() {
@Override
public void run() {
try {
sw.allstop("t1");
} catch (Exception e) {
}
}
};
Thread t2=new Thread() {
@Override
public void run() {
try {
sw.allexceptstop("t2");
} catch (Exception e) {
}
}
};
Thread t3=new Thread() {
@Override
public void run() {
try {
sw.thankyou("t3");
} catch (Exception e) {
}
}
};
t1.setPriority(3);
t2.setPriority(2);
t3.setPriority(1);
t1.start();
t2.start();
t1.join(); //waiting for both t1, t2 to finish
t2.join();
t3.start();
}
}
Output:
ACTIVITY – 10
Q1)
Code:
package act10q1;
import java.util.Arrays;
interface ArraySort {
void apply(String[] arr);
}
public class Act10q1 {
public static final ArraySort sort1 = arr -> {
int n = arr.length;
for (int i = 1; i < n; i++) {
String temp = arr[i];
int j = i - 1;
while (j >= 0 && temp.length() < arr[j].length()) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = temp;
}
System.out.print("Strings sorted in ascending order based on their lengths: ");
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
System.out.print("\n");
System.out.print("\n");
};
public static final ArraySort sort2 = arr -> {
int n = arr.length;
for (int i = 1; i < n; i++) {
String temp = arr[i];
int j = i - 1;
while (j >= 0 && temp.length() > arr[j].length()) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = temp;
}
System.out.print("Strings sorted in descending order based on their lengths: ");
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
System.out.print("\n");
System.out.print("\n");
};
public static final ArraySort fsort = arr -> {
Arrays.sort(arr, (str1, str2) -> str1.charAt(0) -
str2.charAt(0));
Arrays.asList(arr).forEach(System.out::println);
};
public static final ArraySort esort = arr -> {
int n = arr.length;
String arr1[] = new String[arr.length];
int j = 0;
for (int i = 0; i < n; i++) {
int k = arr[i].indexOf('e');
if (k != -1) {
arr1[j] = arr[i];
j++;
}
}
for (int i = 0; i < n; i++) {
int k = arr[i].indexOf('e');
if (k == -1) {
arr1[j] = arr[i];
j++;
}
}
System.out.print("Strings sorted in ascending order based on who contains 'e'
first: ");
for (int i = 0; i < n; i++)
System.out.print(arr1[i] + " ");
System.out.print("\n");
System.out.print("\n");
};
public static void main(String[] args) {
String[] str =
{"devjyoti","karan","devraj","rahi","ram","anshula"};
sort1.apply(str);
sort2.apply(str);
System.out.println("Strings sorted based on the first alphabet only: ");
fsort.apply(str);
esort.apply(str);
}
}
Output:
Q2)
Code:
package act10q2;
import java.util.Scanner;
interface Capital {
void apply(String arr);
}
public class Act10q2 {
public static final Capital printCapitalized = arr -> {
int n = arr.length();
char c = Character.toUpperCase(arr.charAt(0));
System.out.print(c + "");
for (int i = 1; i < n; i++) {
if (arr.charAt(i) == ' ') {
i++;
c = Character.toUpperCase(arr.charAt(i));
System.out.print(" " + c + "");
} else {
c = arr.charAt(i);
System.out.print(c + "");
}
}
System.out.print("\n");
};
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String str = input.nextLine();
printCapitalized.apply(str);
}
}
Output:
Q3)
Code:
package act10q3;
import java.util.*;
class books{
String name, author, type;
float price;
void input20BCE2963()
{
System.out.println("Enter Book Name: ");
Scanner input=new Scanner(System.in);
name=input.next();
System.out.println("Enter Author: ");
author=input.next();
System.out.println("Enter Book Type: ");
type=input.next();
System.out.println("Enter Price: ");
price=input.nextInt();
}
void display20BCE2963()
{
System.out.println("Book Name: "+name);
System.out.println("Author: "+author);
System.out.println("Book Type: "+type);
System.out.println("Price: "+price);
}
}
public class Act10q3 {
public static void main(String[] args) throws InputMismatchException{
List<books> fiction=new ArrayList<books>();
List<books> comic=new ArrayList<books>();
List<books> cooking=new ArrayList<books>();
books b1=new books(),b2=new books(),b3=new books(),b4=new
books(),b5=new books();
b1.input20BCE2963();
b2.input20BCE2963();
b3.input20BCE2963();
b4.input20BCE2963();
b5.input20BCE2963();
if(b1.type=="fiction")
fiction.add(b1);
else if(b1.type=="comic")
comic.add(b1);
else
cooking.add(b1);
if(b2.type=="fiction")
fiction.add(b2);
else if(b2.type=="comic")
comic.add(b2);
else
cooking.add(b2);
if(b3.type=="fiction")
fiction.add(b3);
else if(b3.type=="comic")
comic.add(b3);
else
cooking.add(b3);
if(b4.type=="fiction")
fiction.add(b4);
else if(b4.type=="comic")
comic.add(b4);
else
cooking.add(b4);
if(b5.type=="fiction")
fiction.add(b5);
else if(b5.type=="comic")
comic.add(b5);
else
cooking.add(b5);
System.out.println("Fiction Books:");
for(books b:fiction)
b.display20BCE2963();
System.out.println("Comics:");
for(books b:comic)
b.display20BCE2963();
System.out.println("Cooking Books:");
for(books b:cooking)
b.display20BCE2963();
}
}
Output:
Q4)
Code:
package act10q4;
import java.util.*;
public class Act10q4 {
public static void main(String[] args) {
Map<String, List<String>> h1=new HashMap<String, List<String>>();
Map<String, String>h2=new HashMap<String, String>();
Scanner scan=new Scanner(System.in);
System.out.print("ENTER TOTAL NUMBER OF STUDENTS: "); int
n=scan.nextInt(); scan.nextLine();
List<String> facultyList=new ArrayList<String>();
for(int i=0;i<n;i++)
{
System.out.println("NAME OF STUDENT: ");
String name=scan.nextLine();
System.out.print("TOTAL NUMBER OF COURSES TAKEN BY " +name+" :");
int t=scan.nextInt();
scan.nextLine();
List<String> list=new ArrayList<String>();
for(int j=0;j<t;j++)
{
System.out.print("COURSE"+(j+1)+" NAME : ");
String course=scan.nextLine();
if(!(h2.containsKey(course)))
{
System.out.print("FACULTY NAME FOR "+course +" : ");
String faculty=scan.nextLine();
h2.put(course, faculty);
}
list.add(course);
}
h1.put(name,list);
}
System.out.println(" STUDENT NAME"+ " COURSES REGISTERED");
for(Map.Entry<String, List<String>> entry:h1.entrySet())
{
System.out.println("| "+entry.getKey()+" | "+entry.getValue()+" | ");
}
System.out.println();
System.out.println(" COURSE NAME"+" FACULTY NAME");
for(Map.Entry<String, String> entry:h2.entrySet()){
System.out.println("| "+entry.getKey()+" | "+entry.getValue()+" | ");
}
System.out.println();
System.out.println("FACULTY DETAILS FOR EVERY STUDENT");
for(Map.Entry<String, List<String>> entry:h1.entrySet())
{
System.out.println("Faculties for "+entry.getKey()+" are:");
List<String> str=entry.getValue();
int len=str.size();
for(int i=0;i<len;i++)
{
String sub=str.get(i);
System.out.println(h2.get(sub));
}
System.out.println();
System.out.println("20BCE2963");
}
scan.close();
}
}
Output:
ACTIVITY – 11
Q1)
Code:
package act11q1;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
@Override
public void start(Stage primaryStage) throws Exception {
Button red = new Button("RED");
Button green = new Button("GREEN");
Button blue = new Button("BLUE");
Button pink = new Button("PINK");
Button black = new Button("BLACK");
Font font = Font.font("Courier New", FontWeight.BOLD, 20);
red.setFont(font);
green.setFont(font);
blue.setFont(font);
pink.setFont(font);
black.setFont(font);
Label l1 = new Label("Mounika\n20BCE2963");
l1.setFont(Font.font("Courier New", FontWeight.BOLD, 15));
red.setStyle("-fx-background-color: red");
green.setStyle("-fx-background-color: green");
blue.setStyle("-fx-background-color: blue");
pink.setStyle("-fx-background-color: pink");
black.setStyle("-fx-background-color: black");
red.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event){
System.out.println("RED was clicked");
}});
green.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event){
System.out.println("GREEN was clicked");
}});
blue.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event){
System.out.println("BLUE was clicked");
}});
pink.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event){
System.out.println("PINK was clicked");
}});
black.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event){
System.out.println("BLACK was clicked");
}});
GridPane gp = new GridPane();
gp.setVgap(5);
gp.setHgap(5);
gp.setAlignment(Pos.CENTER);
gp.add(red,2,0);
gp.add(green,2,1);
gp.add(blue,2,2);
gp.add(pink,2,3);
gp.add(black,2,4);
gp.add(l1,0,2);
}
}
Output:
Q2)
Code:
package act11q2;
import javafx.stage.Stage;
import java.time.LocalDate;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
@Override
public void start(Stage primaryStage) throws Exception {
Label l0=new Label("Aryan Chandrakar's Airline");