Professional Documents
Culture Documents
i. USN
ii. Name
iii. Branch
iv. Phone
Write a Java program to create n Student objects and print the USN, Name, Branch, and Phone of
these objects with suitable headings.
Aim:
The aim of this program is to create a class and make objects of that class to print the details ofa student.
Implementation:
Algorithm:
//Input: Values for USN, name, branch and phone no
//Output: Displaying the details of n student objects
//Steps:
1) class “student” is created
2) Declare variables USN, Name, Branch, and Phone no.
3) a constructor of Student class is created to initialize these variable.
4) multiple objects of “student” class is created and print the details contained in student class.
Program :
package soumya;
// https://www.geeksforgeeks.org/java-util-package-java/
import java.util.Scanner;
// Scanner class in Java is used for taking input of all the data types from the user.
int n=input.nextInt();
Student[] s=new Student[n];//array of objects definition
System.out.println("enter student details:");
for(int i=0;i<n;i++)
{
System.out.println((i+1)+"Student details:");
System.out.println("USN");
String USN=input.next();
System.out.println("Name");
String Name=input.next();
System.out.println("Branch");
String Branch=input.next();
System.out.println("Phone");
Long Phone=input.nextLong();
}
System.out.println("Student Details:");
System.out.println("USN"+"\t"+"Name"+"\t"+"Branch"+"\t"+"Phone");
System.out.println("-----------------------------");
for(int i=0;i<n;i++)
{
System.out.println(s[i].USN+"\t"+s[i].Name+"\t"+s[i].Branch+"\t"+s[i].Phone);
}
}
}
output:
4cs34
Name
soumya
Branch
cse
Phone
90876543
2Student details:
USN
4cs56
Name
shruthi
Branch
cse
Phone
4532167895
Student Details:
USN Name Branch Phone
-----------------------------
4cs34 soumya cse 90876543
4cs56 shruthi cse 4532167895
1B. Write a Java program to implement the Stack using arrays. Write Push(), Pop(), and Display()
methods to demonstrate its working.
Aim:
The aim of this program is to create stack using arrays and perform all the stack relatedfunctions like pushing
elements, popping elements and displaying the contents of stack. Stack isabstract data type which demonstrates
Last in first out (LIFO) behavior. We will implement samebehavior using Array.
Implementation:
• A class is created that contains the defined array as well as all the variables defined
• Constructor initialize those variables and array
• Function are created for pushing he elements into stack
• Function are created for popping the elements from stack
• Function are created for displaying the contents of stack
Algorithm:
Steps:
1) A class created and variables are defined like size, array[], top
2) A customized constructor of same class is used for initializing size, top variables
and the array[]
3) A function created for pushing the elements into stack :
push(int pushedElemnet)
{
If(stack is not full)
{
Top++;
Array[top]=pushedElement;
}
Else
{
Stack is full
}
}
4) A function created for popping the elements from stack :
Pop()
{
If(stack is not empty)
{
A=top;
Top--;
}
Else
{
Stack is empty
}
}
5) A function is created for displaying the elements in the stack:
printElemnts()
{
If(top>=0)
{
For(i=0;i<=top;i++)
{
Print all elements of array
}
}
Program:
package soumya;
import java.util.Scanner;
public class lab1b
{
int top;int size;
int[] stk;
publiclab2b()
{
size=3;
top=-1;
stk=new int[10];
}
@SuppressWarnings("resource")
void push()
{
if(top>=size-1)
{
System.out.println("stack overflow");
return;
}
top=top+1;
Scanner input=new Scanner(System.in);
System.out.println("enter an element to push");
stk[top]=input.nextInt();
}
void pop()
{
if(top==-1)
{
System.out.println("stack underflow");
return;
}
int del_item=stk[top];
top=top-1;
System.out.println("popped item="+del_item);
}
void display()
{
if(top==-1)
{
System.out.println("stack underflow");
return;
}
for(int i=top;i>=0;i--)
{
System.out.println("stack["+i+"]"+stk[i]);
}
}
@SuppressWarnings("resource")
public static void main(String args[])
{
lab2b obj=new lab2b();
Scanner input=new Scanner(System.in);
for(;;)
{
System.out.println("---Stack Operation---");
System.out.println("1:push\n 2:pop\n 3:display\n 4:exit");
System.out.println("enter your choice:");
int choice=input.nextInt();
switch(choice)
{
case 1:obj.push();
break;
case 2:obj.pop();
break;
case 3:obj.display();
break;
default:System.out.println("invalid choice");
System.exit(1);
}
}
}
}
Output:
---Stack Operation---
1:push
2:pop
3:display
4:exit
enter your choice:
1
enter an element to push
3
---Stack Operation---
1:push
2:pop
3:display
4:exit
enter your choice:
1
enter an element to push
6
---Stack Operation---
1:push
2:pop
3:display
4:exit
enter your choice:
3
stack[1]6
stack[0]3
---Stack Operation---
1:push
2:pop
3:display
4:exit
enter your choice:
2
popped item=6
---Stack Operation---
1:push
2:pop
3:display
4:exit
enter your choice:
2
popped item=3
---Stack Operation---
1:push
2:pop
3:display
4:exit
enter your choice:
1
enter an element to push
3
---Stack Operation---
1:push
2:pop
3:display
4:exit
enter your choice:
2
popped item=3
---Stack Operation---
1:push
2:pop
3:display
4:exit
enter your choice:
3
stack underflow
---Stack Operation---
1:push
2:pop
3:display
4:exit
enter your choice:
2
stack underflow
---Stack Operation---
1:push
2:pop
3:display
4:exit
enter your choice:
3
stack underflow
---Stack Operation---
1:push
2:pop
3:display
4:exit
2A. Design a superclass called Staff with details as StaffId, Name, Phone, Salary. Extend this class
by writing three subclasses namely Teaching (domain, publications), Technical (skills), and Contract
(period). Write a Java program to read and display at least 3 staff objects of all three categories.
Aim:
Understanding the concepts of inheritance and accessing the members of super class and sub class
Implementation:
A class created named “staff”
Variables are declared in the class like StaffId, Name, PhoneNo, Salary.
Constructor is used to initialize the values of these variables.
Three more classes named “Teaching”, “Technical”, and “Contract” are created that inherits the features of
super class “staff”
Function is created to display 3 staff objects of all three classes.
Algorithm:
//Input: StaffId, Name, Phone and Salary
//Output: displaying the three staff objects of each class.
Steps:
1) A class created named “staff”.
2) Variables are declared in the class like StaffId, Name, PhoneNo, Salary.
3) Constructor is used to initialize the values of these variables.
4) Function is created to read values from console of the variables:
readValues()
{
Scanner s1 new Scanner(System.in);
// Scanner class is defined in library in “java.util.Scanner” package. We need to import
//this package in our program for successful compilation.
System.out.println(“enter staff id”);
StafId= (s1.next());
System.out.println(“enter name”);
name= (s1.next());
System.out.println(“enter phone”);
phone= (s1.next());
System.out.println(“enter salary”);
salary= (s1.nextInt());
}
5) Subclass “teaching” is created that inherits superclass “staff”.
6) Two functions are defined inside “teaching” class. They are “domain” and“publications”. These functions print
the domain of particular staff and no. ofpublications by him respectively.
7) A class is created “technical” that inherits super class “staff”.
8) One function is defined under “technical” class named “skills”. This functions displaysthe skills of particular
staff.
9) A class is created named “contract” that inherits the super class “staff”
10) One function is defined under “contract” class named “period”. This function displayedthe contract period of the
particular staff.
11) From the main method, objects are made of each subclass and functions are called.
Public static void main(String args[])
{
Teaching t1= new Teaching();
t1.readValue();
t1.domain();
t1.publications():
Technical t2=new Technical();
t2.readValue();
t2.skills();
Contract c1=new Contract();
c1.readValue();
c1.period();
}
Program:
package newstaffproj;
import java.util.Scanner;
class Staff
{
String StaffID, Name, Phone, Salary;
Scanner input = new Scanner(System.in);
void read()
{
System.out.println("Enter StaffID");
StaffID = input.nextLine();
System.out.println("Enter Name");
Name = input.nextLine();
System.out.println("Enter Phone");
Phone = input.nextLine();
System.out.println("Enter Salary");
Salary = input.nextLine();
}
void display()
{
System.out.printf("\n%-15s", "STAFFID: ");
System.out.printf("%-15s \n", StaffID);
System.out.printf("%-15s", "NAME: ");
System.out.printf("%-15s \n", Name);
System.out.printf("%-15s", "PHONE:");
System.out.printf("%-15s \n", Phone);
System.out.printf("%-15s", "SALARY:");
System.out.printf("%-15s \n", Salary);
}
}//end of Staff superclass
void read_Teaching()
{
super.read(); // call super class read method
System.out.println("Enter Domain:");
Domain = input.nextLine();
System.out.println("Enter Publication:");
Publication = input.nextLine();
}
void display()
{
super.display(); // call super class display() method
System.out.printf("%-15s", "DOMAIN:");
System.out.printf("%-15s \n", Domain);
System.out.printf("%-15s", "PUBLICATION:");
System.out.printf("%-15s \n", Publication);
}
}//end of Teaching subclass
void read_Technical()
{
super.read(); // call super class read method
System.out.println("Enter Skills:");
Skills = input.nextLine();
}
void display()
{
super.display(); // call super class display() method
System.out.printf("%-15s", "SKILLS:");
System.out.printf("%-15s \n", Skills);
}
}//end of Technical class
void read_Contract()
{
super.read(); // call super class read method
System.out.println("Enter Period");
Period = input.nextLine();
}
void display()
{
super.display(); // call super class display() method
System.out.printf("%-15s", "PERIOD:");
System.out.printf("%-15s \n", Period);
}
}//end of Contract subclass
class DemoStaff
{
public static void main(String[] args)
{
input.close();
}//end of main method
}//end of class DemoStaff
output:
Enter number of staff details to be created
2
Enter Teaching staff information
Enter StaffID
cs34
Enter Name
smriti
Enter Phone
9023456712
Enter Salary
20000
Enter Domain:
cs
Enter Publication:
tcs
Enter Teaching staff information
Enter StaffID
cs45
Enter Name
shruti
Enter Phone
5642134567
Enter Salary
25000
Enter Domain:
cs
Enter Publication:
rhi
Enter Technical staff information
Enter StaffID
2
Enter Name
deepu
Enter Phone
5432134567
Enter Salary
4500
Enter Skills:
c
Enter Technical staff information
Enter StaffID
2
Enter Name
deepak
Enter Phone
4532167865
Enter Salary
5600
Enter Skills:
c++
Enter Contract staff information
Enter StaffID
35
Enter Name
gowri
Enter Phone
4532167890
Enter Salary
4500
Enter Period
3
Enter Contract staff information
Enter StaffID
26
Enter Name
reema
Enter Phone
3214567892
Enter Salary
43200
Enter Period
4
STAFF DETAILS:
-----TEACHING STAFF DETAILS-----
STAFFID: cs34
NAME: smriti
PHONE: 9023456712
SALARY: 20000
DOMAIN: cs
PUBLICATION: tcs
STAFFID: cs45
NAME: shruti
PHONE: 5642134567
SALARY: 25000
DOMAIN: cs
PUBLICATION: rhi
STAFFID: 2
NAME: deepak
PHONE: 4532167865
SALARY: 5600
SKILLS: c++
-----CONTRACT STAFF DETAILS-----
STAFFID: 35
NAME: gowri
PHONE: 4532167890
SALARY: 4500
PERIOD: 3
STAFFID: 26
NAME: reema
PHONE: 3214567892
SALARY: 43200
PERIOD: 4
2B .Write a Java class called Customer to store their name and date_of_birth. The date_of_birth
format should be dd/mm/yyyy. Write methods to read customer data as <name, dd/mm/yyyy> and
display as <name, dd, mm, yyyy> using StringTokenizer class considering the delimiter character
as “/”.
Aim:
Understanding the concepts of StringTokenizer class and separating the strings on the basis ofdifferent delimiters.
Implementation:
Create a class named “customer”.
Store the values of name and date of birth in the variables named as “name” and “date_of_birth”
Create an object of StringTokennizer class and call the methods to separate the strings separated by “/”
delimiter.
Display the name and date of birth in the format specified as the output.
Algorithm:
//input: name and dtae_of_birh in dd/mm/yyy format
//Output: name and date of birth displayed in <name, dd, mm, yyy> format
Steps:
1) A class named “customer” is created.
2) Variables are defined as name and date_of_birth
3) Values are read from the user of name and date of birth in dd/mm/yyy format as strings.
Scanner sc = new Scanner(System.in);
System.out.println("enter name");
String name=sc.next();
System.out.println("enter date of birth in dd/mm/yyy format");
String date = sc.next();
4) A object is created of StringTokenizer class and is used for calling the method of this
class.
5) The method “hasmoreelements” and “nextElement” is used for extracting the strings
separated by “/” delimeter
StringTokenizer strToken = new StringTokenizer(date, "/");
6) The output is displayed then in the form of <name, dd, mm, yyy>
Program:
package soumya;
import java.util.Scanner;
import java.util.StringTokenizer;
public class lab2b {
String name;
String date_of_birth;
void display(String name,String date)
{
StringTokenizer to=new StringTokenizer(date,"/");
String temp=to.nextToken();
int dd= Integer.parseInt(temp);
temp=to.nextToken();
int mm=Integer.parseInt(temp);
temp=to.nextToken();
int yyyy=Integer.parseInt(temp);
System.out.println(name+","+dd+","+mm+","+yyyy);
}
void readCust()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter name");
String name=sc.next();
System.out.println("enter date of birth");
String date=sc.next();
display(name,date);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
lab2 obj=new lab2();
obj.readCust();
}
}
output:
enter name
soumya
enter date of birth
18/11/1985
soumya,18,11,1985
3A. Write a Java program to read two integers a and b. Compute a/b and print, when b is not zero.
Raise an exception when b is equal to zero.
AIM:
Understanding the concepts of exception handling in java
Implementation:
A class is created containing the main method
Inside the main method the calculation is done of division using two operands
Try and Catch block is implemented for handling the arithmetic exception raised when division is done by zero
Else the final output is printed
Algorithm:
// Input: values of two operand i.e a and b
// Output: a) answer displayed when b != 0
b) Arithmetic exception raised and error message displayed when b = 0.
Steps :
1) A class is created containing the main method
2) Two variables are declared i.e. a and b
3) Input is obtained from console
Scanner sc = new Scanner(System.in);
a = sc.nextInt();
b = sc.nextInt();
4) The code to calculate division is kept under try block
try{
System.out.println(a/b);
}
5) The arethemetic exception raised when b=0 is handled in catck block that follows try
block
catch(ArithmeticException e){
e.printStackTrace();
}
Program:
import java.util.Scanner;
}
else
throw new ArithmeticException("Not valid");
}
OUTPUT:
Aim:
To understand the concepts of multithreading by creating three threads that perform different taskswhen one thread
is suspended for some time duration.
Implementation:
Create three class, one for each thread to work
First class is to generate a random number for every 1 second, second class computes the square of the number
and the last class generates cube of the number. All the classes prints the number after generation.
Algorithm:
// Input: Random number
//Output: square and cube of the number
Steps:
1. Three threads are created.
2. Three classes RandomNumber, SquareNum and CubeNum are created
3. Class RandomNumber generates an integer using random number generator and prints theinteger with thread t1
4. Next class SquareNum is called to generate square of the number and print it withthread t2.
5. At last class CubeNum is called to generate cube of the number and print it withthread t3.
Program:
package soumya;
import java.util.Random;
import java.util.Random;
import java.util.Scanner;
class Random_Num_Thread extends Thread {
public void run() {
Random rand;
int i = 0;
do {
rand = new Random();
System.out.print(rand.nextInt(100)+" ");
i++;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (i < 10);
}
}
class Square_Num extends Thread {
int n;
public void run() {
System.out.println("Square of a Number "+this.n+" is:"+Math.pow(this.n, 2));
}
public Square_Num(int a) {
this.n = a;
}
}
class Cube_Num extends Thread {
int n;
public void run() {
System.out.println("\nCube of a Number "+this.n+" is:"+Math.pow(this.n, 3));
}
public Cube_Num(int a) {
this.n = a;
}
}
public class Lab3B {
public static void main(String[] args) {
//Scanner sc=new Scanner(System.in);
Output:
26
Cube of a Number 2 is:8.0
Square of a Number 2 is:4.0
54 40 97 88 79 78 79 34 18