Lab Manual PCS 408 "Java Programming" (B-Tech CSE IV Semester) 2021-2022
Lab Manual PCS 408 "Java Programming" (B-Tech CSE IV Semester) 2021-2022
“Java Programming”
(B-tech CSE IV Semester)
2021-2022
Output
class StaticExamplee
{
static String university;
int Rollno;
static
{
System.out.println("initialising static variable in static block");
university="GEHU";
}
StaticExamplee(int x)
{
Rollno=x;
}
static void StaticDisplay()
{
System.out.println("it will only print static
variable:\nuniversity="+university);
}
void display()
{
System.out.println("Roll no= "+Rollno);
}
}
public class Q2 {
}
}
import java.util.*;
public class Q3 {
public static void main(String args[])
{
int PurchaseAmount;
int choice;
float DiscountMill=0;
float DiscountHandl=0;
float totalHandl=0;
float totalMill = 0;
Scanner input= new Scanner(System.in);
float finalAmount;
float totalDiscount;
do
{
System.out.println("enter yout choice 1. to purchase mill cloth 2. to
purchase handloom items 3. to calculate bill");
choice= input.nextInt();
switch(choice)
{
case 1:
System.out.println(" enter purchase amount:");
PurchaseAmount=input.nextInt();
if(PurchaseAmount<=100)
{
DiscountMill=0;
totalMill=PurchaseAmount-DiscountMill;
}
else if(PurchaseAmount<=200)
{
DiscountMill=(PurchaseAmount)*0.05f;
totalMill=PurchaseAmount-DiscountMill;
}
else if(PurchaseAmount<=300)
{
DiscountMill=(PurchaseAmount)*0.075f;
totalMill=PurchaseAmount-DiscountMill;
}
else
{
DiscountMill=(PurchaseAmount)*0.1f;
totalMill=PurchaseAmount-DiscountMill;
DiscountHandl=(PurchaseAmount)*0.075f;
totalHandl=PurchaseAmount-DiscountHandl;
}
else if(PurchaseAmount<=300)
{
DiscountHandl=(PurchaseAmount)*0.1f;
totalHandl=PurchaseAmount-DiscountHandl;
}
else
{
DiscountHandl=(PurchaseAmount)*0.15f;
totalHandl=PurchaseAmount-DiscountHandl;
}
break;
}while(choice!=3);
finalAmount= totalHandl+totalMill;
totalDiscount=DiscountHandl+DiscountMill;
System.out.println("************************************");
System.out.println("Written By: Aryan Panwar 13 CSE(4)J");
System.out.println("*************************************");
System.out.println("finalamount="+finalAmount);
System.out.println("totalDiscount="+(float)totalDiscount);
input.close();
import java.util.*;
public class Q4 {
public static void main(String args[]){
System.out.println("************************************");
System.out.println("Written By: Aryan Panwar 13 CSE(4)J");
System.out.println("*************************************");
Scanner in = new Scanner(System.in);
String str;
String strcount;
System.out.println("enter string:");
str=in.nextLine();
System.out.println("enter substring:");
strcount=in.nextLine();
int count = ( str.split(strcount).length );
System.out.println("Total occurrences: " + count);
in.close();
}
}
Output
public class Q5 {
public static void main(String[] args) {
System.out.println("************************************");
System.out.println("Written By: Aryan Panwar 13 CSE(4)J");
System.out.println("*************************************");
String str1 = "Graphic Era";
System.out.println("The given string is: " + str1);
System.out.println("After removing duplicates characters the new
string is: " + removeDuplicateChars(str1));
}
private static String removeDuplicateChars(String sourceStr) {
char[] arr1 = sourceStr.toCharArray();
String targetStr = "";
for (char value: arr1) {
if (targetStr.indexOf(value) == -1) {
targetStr += value;
}
}
return targetStr;
}
}
Output
import java.util.*;
public class Q6 {
public static void main(String[] args) {
System.out.println("************************************");
System.out.println("Written By: Aryan Panwar 13 CSE(4)J");
System.out.println("*************************************");
String str1 = "Graphic Era";
System.out.println("The given string is: " + str1);
System.out.println("After removing duplicates characters the new
string is: " + removeDuplicateChars(str1));
}
private static String removeDuplicateChars(String sourceStr) {
char[] arr1 = sourceStr.toCharArray();
String targetStr = "";
for (char value: arr1) {
if (targetStr.indexOf(value) == -1) {
targetStr += value;
}
}
return targetStr;
}
}
Output
import java.util.*;
public class Q7 {
do
{
System.out.println("enter your
choice:\n1.Delhi\n2.Mumbai\n3.Chennai\n4.Kolkata\n5.to exit");
citychoice=input.nextInt();
switch (citychoice)
{
case 1:
do
{
System.out.println("enter your
choice:\n1.Maruti-k10\n2.Zen-Astelo\n3.WagonR\n4.Maruti-SX4\n5.reset\n");
carchoice=input.nextInt();
switch (carchoice)
{
case 1:
arr[citychoice-1][carchoice-1]++;
break;
case 2:
arr[citychoice-1][carchoice-1]++;
break;
case 3:
arr[citychoice-1][carchoice-1]++;
break;
case 4:
arr[citychoice-1][carchoice-1]++;
break;
default:
}
}while(carchoice==5);
break;
case 2:
do
{
System.out.println("enter your
choice:\n1.Maruti-k10\n2.Zen-Astelo\n3.WagonR\n4.Maruti-SX4\n5.reset city\n");
carchoice=input.nextInt();
switch (carchoice)
{
case 1:
arr[citychoice-1][carchoice-1]++;
break;
case 2:
arr[citychoice-1][carchoice-1]++;
break;
case 3:
arr[citychoice-1][carchoice-1]++;
break;
case 4:
arr[citychoice-1][carchoice-1]++;
break;
default:
break;
}
}while(carchoice==5);
break;
case 3:
do
{
System.out.println("enter your choice:\n1.Maruti-k10\n2.Zen-
Astelo\n3.WagonR\n4.Maruti-SX4\n5.exit\n");
carchoice=input.nextInt();
switch (carchoice)
{
case 1:
arr[citychoice-1][carchoice-1]++;
break;
case 2:
arr[citychoice-1][carchoice-1]++;
break;
case 3:
arr[citychoice-1][carchoice-1]++;
break;
case 4:
arr[citychoice-1][carchoice-1]++;
Prepared by – Aryan Panwar Roll no.-13 Sec-J
break;
default:
break;
}
}while(carchoice==5);
break;
case 4:
do
{
System.out.println("enter your choice:\n1.Maruti-k10\n2.Zen-
Astelo\n3.WagonR\n4.Maruti-SX4\n5.exit\n");
carchoice=input.nextInt();
switch (carchoice)
{
case 1:
arr[citychoice-1][carchoice-1]++;
break;
case 2:
arr[citychoice-1][carchoice-1]++;
break;
case 3:
arr[citychoice-1][carchoice-1]++;
break;
case 4:
arr[citychoice-1][carchoice-1]++;
break;
default:
break;
}
}while(carchoice==5);
break;
default:
break;
}
}while(citychoice!=5);
int i = 0;i<arr.length;i++)
{
System.out.println("--------------------------------------------------
---------------");
System.out.print(city[i] + " ");
for (int j = 0; j < arr[i].length; j++)
{
System.out.print(arr[i][j] + " ");
Prepared by – Aryan Panwar Roll no.-13 Sec-J
}
System.out.println();
} System.out.println("-----------------------------------------------
------------------");
input.close();
}
Output
import java.util.*;
public class Q8 {
public static void main(String Args[]) {
System.out.println("************************************");
System.out.println("Written By: Aryan Panwar 13 CSE(4)J");
System.out.println("*************************************");
int[][] arr = new int[12][12];
}
int i = 0, j = 0;
}
}
}
System.out.println();
}
System.out.println("-----------------------------------------------------
------------");
if(i==10)
{
System.out.print("marks ");
}
if(i==11)
{
System.out.print("roll no ");
}
for(j=0;j<arr[i].length-9;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
Prepared by – Aryan Panwar Roll no.-13 Sec-J
System.out.print("student who obtained heighest total marks:"+arr[11][3]);
input.close();
}
}
Output
import java.util.*;
public class Q9 {
public static void main(String[] args)
{
System.out.println("************************************");
System.out.println("Written By: Aryan Panwar 13 CSE(4)J");
System.out.println("*************************************");
int[][] city = new int[10][5];
String[] name = { "delhi", "mumbai", "chennai", "kolkata",
"dehradun"};
String cityname;
Scanner input = new Scanner(System.in);
System.out.println("enetr day-wise temp of all cities");
for (int i = 0; i < city.length; i++)
{
System.out.println(" day"+(i+1));
}
if(city[i][column]>=max)
{
max = city[i][column];
maxday=i+1;
}
}
System.out.println();
Prepared by – Aryan Panwar Roll no.-13 Sec-J
}
System.out.println("------------------------------------------------
-----------------");
input.close();
}
}
Output
import java.util.*;
do
{
System.out.println("enter choice\n1.to vote candidate1\n2.to
candidate2\n3.to vote candidate3\n4.to vote candidate4\n5.to vote
candidate5\nelse NOTA");
choice=input.nextInt();
switch(choice)
{
case 1:
arr[0]++;
break;
case 2:
arr[1]++;
break;
case 3:
arr[2]++;
break;
case 4:
arr[3]++;
break;
case 5:
arr[4]++;
break;
default:
arr[5]++;
break;
}
Output
import java.util.*;
}
else{
}
int j=1;
for(int i=1; i<arr.length; i++)
{
if(arr[i-1]==arr[i])
{
Arrcount[j]++;
}
else
j++;
int max=Arrcount[0];
for(int i=1; i<Arrcount.length; i++)
{
if(Arrcount[i]>max)
}
}
Output
import java.util.*;
class Telcall{
long phno;
String sname;
int n;
float amt;
void compute()
{
if(n<100)
{
amt=500;
}
else if(n<200){
amt=500+1*(n-100);
}
else if(n<300)
{
amt=600+1.20f*(n-200);
}
else
{
amt=720+1.50f*(n-300);
}
System.out.println("amount:"+amt);
}
Telcall(String cust_name,long contact,int nocall)
{
phno =contact;
sname=cust_name;
n=nocall;
}
}
public class Q12 {
public static void main(String[] args)
{
System.out.println("************************************");
System.out.println("Written By: Aryan Panwar 13 CSE(4)J");
System.out.println("*************************************");
Scanner input = new Scanner(System.in);
String cust_name;
long contact;
}
}
Output
import java.util.*;
class ObjArray
{
String name;
int RollNo;
ObjArray(String S, int R)
{
name = S;
RollNo = R;
}
void show()
{
}
}
public class Q13 {
public static void main(String[] args)
{
System.out.println("************************************");
System.out.println("Written By: Aryan Panwar 13 CSE(4)J");
System.out.println("*************************************");
Scanner input = new Scanner(System.in);
ObjArray []arr=new ObjArray[5];
int RollNo;
String name;
for(int i=0;i<arr.length;i++)
{
RollNo=input.nextInt();
name=input.nextLine();
arr[i]=new ObjArray(name, RollNo);
}
System.out.println(" name " + " Rollno");
for(int i=0;i<arr.length;i++)
{System.out.println("---------------");
arr[i].show();
}
System.out.println("---------------");
input.close();
}
}
class A
{
public void show()
{
System.out.println(" hi i am public method");
}
protected void print()
{
System.out.println(" hi i am protected method");
}
class parent
{
String name;
int age;
parent(String s, int n)
{
name = s;
age = n;
}
}
void display()
{
System.out.println("parent info");
System.out.println("age=" + super.age);
System.out.println("name=" + super.name);
System.out.println("child info");
System.out.println("age=" + age);
System.out.println("name=" + name);
}
public class Q15 {
public static void main(String args[])
{
System.out.println("************************************");
System.out.println("Written By: Aryan Panwar 13 CSE(4)J");
System.out.println("*************************************");
child ch1=new child("mohan",65,"rohan",34);
ch1.display();
}
interface Speedclass{
int speed=90;
public void distance();
}
interface Distanceclass{
int distance=100;
}
MasterCard(String S) {
CardNumber = S;
}
void CheckCard()
{
}
else
System.out.println("invalid");
}
}
class Visa extends CreditCard
{
String CardNumber;
Visa(String S) {
CardNumber = S;
}
void CheckCard()
{
if ((CardNumber.length() == 16||CardNumber.length() ==
13)&&CardNumber.charAt(0)==52)
{
System.out.println("valid ");
String CardNumber;
AmericanExpress(String S) {
CardNumber = S;
}
void CheckCard()
{
{
System.out.println("valid ");
}
else
System.out.println(" invaild");
}
}
Output
Output
Output
}
class PrintOddEven extends Thread {
String num;
PrintOddEven(String num)
{
this.num = num;
}
public void run(){
if(num.equalsIgnoreCase("even"))
{
for(int i =0;i<=500;i=i+2)
{
System.out.println("even"+i);
Output
}
public class Q22 {
public static void main(String args[]) {
System.out.println("topic: Synchronisation ");
Output
Output
import java.awt.*;
import java.awt.event.*;
class Shipment extends Frame implements ActionListener, ItemListener
{
Panel p1,p2,p3,p4;
Label l1,l2,l3;
TextField txt1,txt2;
Button btncal;
Checkbox chkDiscount,chkWholesaler,chkRetailer;
CheckboxGroup cbg;
double cost=0.0;
double temp=0.0;
Shipment()
{
setLayout(new FlowLayout());
setTitle("Company");
setVisible(true);
setSize(500,200);
l1=new Label("Unit Nos");
l2=new Label("Customer Type");
l3=new Label("Cost total", Label.RIGHT);
txt1=new TextField(20);
txt2=new TextField(20);
txt2.setEditable(false);
cbg=new CheckboxGroup();
chkDiscount=new Checkbox("Special Discount");
Prepared by – Aryan Panwar Roll no.-13 Sec-J
chkDiscount.setEnabled(false);
chkWholesaler=new Checkbox("Wholesaler",cbg,true);
chkRetailer=new Checkbox("Retailer",false,cbg);
p2.add(l2);
p2.add(txt2);
p2.add(chkRetailer);
p2.add(chkWholesaler);
p3.add(btncal);
p3.add(l3);
p3.add(txt2);
add(p1);
add(p2);
add(p3);
btncal.addActionListener(this);
chkDiscount.addItemListener(this);
addWindowListener(new WindowAdapter()
{
@Override
Prepared by – Aryan Panwar Roll no.-13 Sec-J
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
public static void main(String[] args)
{
new Shipment();
}
@Override
public void actionPerformed(ActionEvent arg0)
{
int i=Integer.parseInt(txt1.getText());
chkDiscount.setEnabled(true);
if(i>0&&i<=15)
{
if(chkWholesaler.getState())
cost=50*i;
else
cost=60*i;
}
if(i>15&&i<=20)
{
if(chkWholesaler.getState())
cost=45*i;
else
cost=55*i;
Prepared by – Aryan Panwar Roll no.-13 Sec-J
}
if(i>20&&i<=30)
{
if(chkWholesaler.getState())
cost=40*i;
else
cost=50*i;
}
if(i>50)
{
if(chkWholesaler.getState())
cost=30*i;
else
cost=40*i;
}
txt2.setText(cost+"");
temp=cost;
}
@Override
public void itemStateChanged(ItemEvent e)
{
if(chkDiscount.getState())
{
cost=cost-(cost*0.1);
}
else
cost=temp;
txt2.setText(cost+"");
Prepared by – Aryan Panwar Roll no.-13 Sec-J
}
OUTPUT
import java.awt.*;
import java.awt.event.*;
class Match extends Frame implements ActionListener {
String[] country={"INDIA","CANADA","AFGANISTAN","ALBANIA","ALGERIA"};
String[] capital={"NEW DELHI","OTTAWA","Kabul","Tirane","Algiers"};
Choice co=new Choice();
Choice ca=new Choice();
Button b=new Button("Check");
TextField t1=new TextField();
Match() {
for (String s : country) {
co.add(s);
}
for (String s : capital) {
ca.add(s);
}
co.setBounds(40,80,150,50);
ca.setBounds(300,80,150,50);
b.setBounds(180,150,60,50);
t1.setBounds(50,120,300,20);
add(co);
add(ca);
add(b);
add(t1);
setSize(500,250);
setTitle("This is our basic AWT example");
setLayout(null);
setVisible(true);
b.addActionListener(this);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
class Q26 {
public static void main(String[] args) {
new Match();
}
}
OUTPUT
import java.awt.*;
import javax.swing.*;
public class Border
{
JFrame f;
Border()
{
f = new JFrame();
// creating buttons
JButton b1 = new JButton("NORTH");; // the button will be labeled as NORTH
JButton b2 = new JButton("SOUTH");; // the button will be labeled as SOUTH
JButton b3 = new JButton("EAST");; // the button will be labeled as EAST
JButton b4 = new JButton("WEST");; // the button will be labeled as WEST
JButton b5 = new JButton("CENTER");; // the button will be labeled as CENTER
f.setSize(300, 300);
f.setVisible(true);
}
public static void main(String[] args) {
System.out.println("*****************************");
System.out.println("Written By: Aryan Panwar 13 CSE(4)J ");
Prepared by – Aryan Panwar Roll no.-13 Sec-J
System.out.println("*****************************");
new Border();
}
}
OUTPUT
// Default constructor
public Demo(int a, String b)
{
this.a = a;
this.b = b;
}
class Test
{
public static void main(String[] args)
{
System.out.println("**********************************");
System.out.println("Written By: Aryan Panwar 13 CSE(4)J ");
System.out.println("**********************************");
Demo object = new Demo(1, "javaprogramming");
String filename = "file.ser";
// Serialization
try
{
//Saving of object in a file
FileOutputStream file = new FileOutputStream(filename);
ObjectOutputStream out = new ObjectOutputStream(file);
out.close();
file.close();
catch(IOException ex)
{
System.out.println("IOException is caught");
}
// Deserialization
try
{
// Reading the object from a file
FileInputStream file = new FileInputStream(filename);
ObjectInputStream in = new ObjectInputStream(file);
catch(ClassNotFoundException ex)
{
System.out.println("ClassNotFoundException is caught");
}
}
}
import java.sql.*;
import java.util.*;
Output