0% found this document useful (0 votes)
191 views

Code:: Q1:Write A Simple Class Program To Calculate Simple Interest

The document contains 8 questions related to Java programming concepts. Each question provides sample code to demonstrate a concept such as: 1) Calculating simple interest 2) Finding prime numbers within a range 3) Implementing inheritance with the super keyword 4) Using abstract classes 5) Implementing multiple interfaces in a class 6) Using user-defined packages 7) Implementing LinkedList and HashSet classes 8) Implementing stacks and queues using collection classes For each question, the code provides an example and displays the output.

Uploaded by

Shivani Chatte
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
191 views

Code:: Q1:Write A Simple Class Program To Calculate Simple Interest

The document contains 8 questions related to Java programming concepts. Each question provides sample code to demonstrate a concept such as: 1) Calculating simple interest 2) Finding prime numbers within a range 3) Implementing inheritance with the super keyword 4) Using abstract classes 5) Implementing multiple interfaces in a class 6) Using user-defined packages 7) Implementing LinkedList and HashSet classes 8) Implementing stacks and queues using collection classes For each question, the code provides an example and displays the output.

Uploaded by

Shivani Chatte
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Q1 :Write a simple class program to calculate simple interest.

Code:
import java.util.Scanner;
public class Program1
{
public static void main(String args[])
{
float p, r, t, sinterest;
Scanner scan = new Scanner(System.in);
System.out.print("Enter the Principal Amount: ");
p = scan.nextFloat();
System.out.print("Enter the Rate of interest : ");
r = scan.nextFloat();
System.out.print("Enter the Time period : ");
t = scan.nextFloat();
scan.close();
sinterest = (p * r * t) / 100;
System.out.print("Simple Interest is: " +sinterest);
}
}

Output:

1
Q2 : Write a program to display prime numbers between the
given range using command line, BufferedReader and Scanner
class.
Code:
import java.util.Scanner;
import java.io.*;
class Program2
{
public static void main(String args[])throws IOException
{
int type;
Scanner s = new Scanner(System.in);
System.out.println("Press 1 for Command Line");
System.out.println("Press 2 for Buffer Reader");
System.out.println("Press 3 for Scanner Class");
type = s.nextInt();
switch(type)
{
case 1:
int low = Integer.parseInt(args[0]), high =
Integer.parseInt(args[1]);
while (low < high)
{
boolean flag = false;
for(int i = 2; i <= low/2; ++i)
{
if(low % i == 0)
{
flag = true;
break;
}
}
if (!flag && low != 0 && low != 1)

2
{
System.out.print(low + " ");
++low;
}
}
break;
case 2:
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter Low Limit : ");
low = Integer.parseInt(br.readLine());
System.out.println("Enter High Limit : ");
high= Integer.parseInt(br.readLine());
while (low < high)
{
boolean flag = false;
for(int i = 2; i <= low/2; ++i)
{
if(low % i == 0)
{
flag = true;
break;
}
}
if (!flag && low != 0 && low != 1)
System.out.print(low + " ");
++low;
}
break;
case 3:
System.out.println("Enter Low Limit : ");
low = s.nextInt();
System.out.println("Enter High Limit : ");
high= s.nextInt();
3
while (low < high)
{
boolean flag = false;
for(int i = 2; i <= low/2; ++i)
{
if(low % i == 0)
{
flag = true;
break;
}
}
if (!flag && low != 0 && low != 1)
System.out.print(low + " ");
++low;
}
break;
}
}
}

Output:

4
Q3 : Write a program to implement simple inheritance using
super keyword.
Code:
class Animal
{
public void animalSound()
{
System.out.println("The animal makes a sound");
}
}
class Dog extends Animal
{
public void animalSound()
{
super.animalSound();
System.out.println("The dog says: bow wow");
}
}
public class Program3
{
public static void main(String args[])
{
Animal A = new Dog();
A.animalSound();
}
}

5
Output:

6
Q4 :Write a program to demonstrate abstract class.
Code:
abstract class Shape
{
abstract void draw();
}
class Rectangle extends Shape
{
void draw()
{
System.out.println("Drawing Rectangle");
}
}
class Circle extends Shape
{
void draw()
{
System.out.println("Drawing Circle");
}
}
class Program4
{
public static void main(String args[])
{
Shape s=new Circle();
s.draw();
}
}

Output:

7
Q5 :Write a program to implement two interfaces (bank and
customer) in one class (account details).
Code :
interface Bank
{
public void bankInfo();
}
interface Customer
{
public void customerInfo();
}
class AccountDetail implements Customer, Bank
{
public void accountInfo()
{
System.out.println("*****Account Details*****");
}
public void bankInfo()
{
System.out.println("Bank Name :Bank Of Maharashtra ");
System.out.println("IFSC Code :MAHA234 ");
}
public void customerInfo()
{
System.out.println("Account Number: 2345665112");

8
System.out.println("Customer Name: Chatte Shivani");
System.out.println("Account type:Saving");
System.out.println("Adhar Number: 234598726741");
System.out.println("Mobile Number: 9665282622");
}
}
class Program5
{
public static void main(String[] args)
{
AccountDetail A = new AccountDetail();
A.accountInfo();
A.bankInfo();
A.customerInfo();
}
}

Output:

9
Q6 :Write a program to demonstrate user defined package.
Code:
import p.Package1;
class Program6 extends Package1
{
void disp()
{
System.out.println("Beep Beep");
}
public static void main(String[] args)
{
Program6 obj=new Program6();
obj.disp();
int receive=obj.getSum(10,20);
System.out.println("The Result is "+receive);
}
}

Package Code:
package p;
public class Package1
{
public int getSum(int num1, int num2)
{
int result;
result=num1+num2;
return result;
}
}

Output:

10
Q7 : Write a program to implement LinkedList and HashSet
class.
Code:
a) LinkedList:
import java.util.*;
class LinkedDemo
{
public static void main(String args[])
{
LinkedList <String> L = new LinkedList <String> ();
L.add("Apple");
L.addFirst("Banana");
L.addLast("Orange");
System.out.println(L);
ListIterator I = L. listIterator();
while(I.hasNext())
{
System.out.println(I.next());
}
}
}

Output:

11
b) HashSet:
import java.util.*;
class HashDemo
{
public static void main(String args[])
{
HashSet s= new HashSet();
s.add("Orange");
s.add("Mango");
System.out.println(s);
System.out.println(s.size());
Iterator i = s.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
}

Output:

12
Q8 :Write a Program to implement stack and queue using
collection framework.
a) Stack Code :
import java.util.*;
public class StackExample
{
public static void main(String[] args)
{
Stack s= new Stack<>();
boolean result = s.empty();
System.out.println("Is the stack empty? " + result);
s.push(78);
s.push(113);
s.push(90);
s.push(120);
System.out.println("Elements in Stack: " + s);
result = s.empty();
System.out.println("Is the stack empty? " + result);
}
}

Output:

13
b)Queue Code:
import java.util.*;
class QueueExample
{
public static void main(String args[])
{
Queue q=new LinkedList();
q.add("Shivani");
q.add("Sapana");
q.add("Ashish");
q.add("Sushant");
q.add("Suraj");
System.out.println("iterating the queue elements:");
Iterator itr=q.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
q.remove();
q.poll();
System.out.println("after removing two elements:");
Iterator<String> itr2=q.iterator();
while(itr2.hasNext())
{
System.out.println(itr2.next());
}
}
}

14
Output:

15
Q9 :Write a program to demonstrate nested try block.
Code:
import java.util.*;
class NestedTry
{
public static void main(String args[])
{
try
{
int a[] = {30, 45, 60, 75, 90, 105, 120, 140, 160, 200};
System.out.println("Element at index 8 = "+a[8]);
try
{
System.out.println("Inner Try Block");
int res = 100/ 0;
}
catch (ArithmeticException ex2)
{
System.out.println("Sorry! Division by zero isn't
feasible!");
}
System.out.println("Outer Try Block");
}
catch (ArrayIndexOutOfBoundsException ex1)
{
System.out.println("ArrayIndexOutOfBoundsException");
}
}
}

16
Output:

17
Q10 : Write a program to create user defined exception class.
Code:
import java.util.*;
public class UDException
{
static void validate(int age)
{
if(age<18)
throw new ArithmeticException("not valid");
else
System.out.println("welcome to vote");
}
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter the age of the person: ");
int age = sc.nextInt();
validate(age);
System.out.println("rest of the code...");
}
}

Output:

18
Q11 : Write a program to copy content of one file to another file,
read the file name from user.
Code :
import java.io.*;
import java.util.Scanner;
class CopyFile
{
public static void main(String args[])
{
try
{
Scanner s = new Scanner(System.in);
System.out.println("Enter File Name that is to be copy with
extension : ");
FileInputStream fin= new FileInputStream(s.next());
System.out.println("New File Name : ");
FileOutputStream fout = new FileOutputStream(s.next());
int i = 0;
byte[] a = new byte[50];
while((i=fin.read(a))!=-1)
{
fout.write(a);
}

}
catch(Exception e)
{

}
}
}

19
Output :

CopyFile.txt :

20
Q12 : Write a program to create multiple threads to display even
and odd numbers between the given range.
Code:
import java.util.*;
class Even extends Thread
{
int a,b;
Even(int no1, int no2)
{
a=no1;
b=no2;
}
public void run()
{
try
{
System.out.println("-----EVEN NUMBERS-----");
for(int i = a; i <= b; i++)
{
if(i%2==0)
{
System.out.println(i);
}
}
}
catch(Exception e)
{
}
}
}
class Odd extends Thread
{
int a,b;
Odd(int no1, int no2)
{
a=no1;
b=no2;
}
public void run()
{
try
{
System.out.println("-----ODD NUMBERS-----");
for(int i = a; i <= b; i++)

21
{
if(i%2!=0)
{
System.out.println(i);
}
}
}
catch(Exception e)
{
}
}
}
class MultiThread
{
public static void main(String args[])
{
System.out.println("Write a program to create multiple threads to
display even and odd numbers between the given ranges.");
System.out.println("Enter Starting Range : ");
Scanner s = new Scanner(System.in);
int a = s.nextInt();
System.out.println("Enter Ending Range : ");
int b = s.nextInt();
System.out.println("----------");
Even e = new Even(a,b);
Odd o = new Odd(a,b);
e.start();
o.start();
}
}

22
Output:

23
Q 13 : Write a program to display multiplication table of given
numbers using synchronized keyword.
Code:
import java.util.Scanner;
class Multiplication_Table
{
void printTable(int n)
{
synchronized(this)
{
for(int i=1;i<=10;i++)
{
System.out.println(+n+"*"+i+"="+(n*i));
try
{
Thread.sleep(100);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
public static void main(String args[])
{
Multiplication_Table obj = new Multiplication_Table();
Mythread1 th1 = new Mythread1(obj);
th1.start();
}
}

class Mythread1 extends Thread

24
{
Multiplication_Table t;
Mythread1(Multiplication_Table t)
{
this.t=t;
}
public void run()
{
Scanner s = new Scanner(System.in);
System.out.println("Enter Any number for a Table : ");
int a = s.nextInt();
t.printTable(a);
}
}

Output:

25
Q14 : Write a program to create an Applet to calculate factorial
of an entered number.
Code:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="FactApplet" width=400 height=300>
</applet>
*/
public class FactApplet extends Applet implements ActionListener
{
TextField number,factorial;
Button compute;
public void init()
{
Label numberp = new Label("Input Number: ");
Label factorialp = new Label("Factorial: ");
number= new TextField(30);
factorial = new TextField(30);
compute = new Button("Compute");
add(numberp);
add(number);
add(factorialp);
add(factorial);
add(compute);
compute.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String snumber;
snumber = number.getText();
int inumber = Integer.parseInt(snumber);

26
factorial.setText(((Double)getFactorial(inumber)).toString());
}
double getFactorial(int k)
{
double fact = 1;
for(int i=1;i<=k;i++)
fact = fact * i;
return fact;
}
}

Output:

27
Q15 :Write a program to create employee information using GUI
controls.
Code:
import java.awt.*;
class Gui extends Frame
{
Gui()
{
Label l = new Label("Employee Registration ");
l.setBounds(200,30, 500,70);
l.setFont(new Font("Serif",Font.PLAIN,28));
add(l);

Label l1 = new Label("Employee ID : ");


l1.setBounds(50,100, 100,30);
add(l1);

TextField t1;
t1=new TextField();
t1.setBounds(200,100, 100,30);
add(t1);

Label l2 = new Label("Employee Name:");


l2.setBounds(50,150, 100,30);
add(l2);

TextField t2;
t2=new TextField();
t2.setBounds(200,150, 200,30);
add(t2);

Label l3 = new Label("Gender : ");


l3.setBounds(50,200, 100,30);
add(l3);

CheckboxGroup cbg1=new CheckboxGroup();


Checkbox cb1=new Checkbox("Male ",cbg1,false);
cb1.setBounds(200,200, 70,30);
Checkbox cb2=new Checkbox("Female",cbg1,false);
cb2.setBounds(270,200, 200,30);
add(cb1);
add(cb2);

Label l4 = new Label("Department : ");

28
l4.setBounds(50,250, 100,30);
add(l4);

Checkbox checkbox1 = new Checkbox("Account ");


checkbox1.setBounds(200,250, 100,50);
Checkbox checkbox2 = new Checkbox("Marketing");
checkbox2.setBounds(300,250,100,50);
Checkbox checkbox3 = new Checkbox("Production");
checkbox3.setBounds(400,250,100,50);
Checkbox checkbox4 = new Checkbox("Management");
checkbox4.setBounds(500,250,100,50);
add(checkbox1);
add(checkbox2);
add(checkbox3);
add(checkbox4);

Label l5 = new Label("Address : ");


l5.setBounds(50,300, 100,30);
add(l5);

TextArea area=new TextArea("");


area.setBounds(200,300, 200,100);
add(area);

Button b = new Button("Submit");


b.setBounds(300,450,80,30);
add(b);
setSize(800,600);
setLayout(null);
setVisible(true);
}
public static void main(String args[])
{
Gui g = new Gui();
}
}

29
Output:

30

You might also like