Sara Java
Sara Java
92
INPUT:-
import java.util.Scanner;
public class AreaPerimeter
{ public static void main(String[] args)
{ double a,p,r;
System.out.println("Enter radius value");
Scanner sc = new Scanner(System.in);
r=sc .nextDouble();
a=Math.PI*r;
p=2*Math.PI*r;
System.out.println("Area="+a);
System.out.println("Perimeter="+p);
}
}
OUTPUT:-
C:\GargiProject>javac AreaPerimeter.java
C:\GargiProject>java AreaPerimeter
Enter radius value
2
Area=6.283185307179586
Perimeter=12.566370614359172
1
GURU NANAK KHALSA COLLEGE
92
Q2) There are exactly 2.54 centimeters to an inch . Write a program that
takes a number of inches from user and converts it to centimeters.
INPUT:-
import java.util.Scanner;
public class InchesToCentimeters
{ public static void main(String[] args)
{ double cm,in;
System.out.println("Enter the value of inches");
Scanner sc = new Scanner(System.in);
in=sc .nextDouble();
cm = in*2.54;
System.out.println(in +"inches = " + cm + "centimeters");
}
}
OUTPUT:-
C:\GargiProject>javac InchesToCentimeters.java
C:\GargiProject>java InchesToCentimeters
Enter the value of inches
10
10.0inches = 25.4centimeters
2
GURU NANAK KHALSA COLLEGE
92
Q3) Sales tax in some cities is 8.25%.Write a program that accepts a price
from the user and prints out the appropriate tax and total purchase price.
import java.util.Scanner;
scanner.close();
}
}
OUTPUT:-
3
GURU NANAK KHALSA COLLEGE
92
Q4) Write a program to find solution of quadratic equation. Accept a,b and
c from user.
INPUT:-
import java.util.Scanner;
public class CalQuad
{
public static void main(String[] args)
{
double a,b,c,root1,root2;
System.out.println("For given equation ax^2+bx+c");
System.out.println("\nEnter a: ");
Scanner sc = new Scanner(System.in);
a = sc .nextDouble();
System.out.println("\nEnter b: ");
b = sc .nextDouble();
System.out.println("\nEnter c: ");
c = sc .nextDouble();
double d=(b*b)-(4*a*c);
System.out.println("\nDiscrimination= "+d);
if(d>0)
{ System.out.println("Roots are real and they are unequal");
root1 = (-b+Math.sqrt(d))/(2*a);
root2 = (-b-Math.sqrt(d))/(2*a);
System.out.println("\nRoot1= "+root1);
System.out.println("\nRoot2= "+root2);
}
else if (d==0)
{
System.out.println("Roots are real and they are equal");
root1= (-b+Math.sqrt(d))/(2*a);
System.out.println("Root1="+root1);
}
else
4
{
GURU NANAK KHALSA COLLEGE
92
OUTPUT:-
C:\GargiProject>javac CalQuad.java
C:\GargiProject>java CalQuad
For given equation ax^2+bx+c
Enter a:
10
Enter b:
20
Enter c:
30
Discrimination= -800.0
Roots are Imaginary
import java.util.Scanner;
5
// Input number from the user
GURU NANAK KHALSA COLLEGE
92
scanner.close();
}
int sum = 0;
while (number > 0) {
// Extract the last digit
int digit = (int)(number % 10);
sum += digit;
// Remove the last digit from number
number /= 10;
}
return sum;
}
}
OUTPUT:-
6
GURU NANAK KHALSA COLLEGE
92
Q6) Write a program to find Whether Given Year is a Leap Year or Not.
import java.util.Scanner;
scanner.close();
}
7
OUTPUT:-
import java.util.Scanner;
scanner.close();
}
8
public static boolean isVowel(char ch) {
ch = Character.toLowerCase(ch); // Convert to lowercase for case
insensitivity
OUTPUT:-
Enter a character: a
a is a vowel.
Enter a character: 7
7 is a number.
Enter a character: %
% is a special character.
Enter a character: r
r is a consonant.
9
Q8) Write a program to check whether the entered character is uppercase or
lowercase.
import java.util.Scanner;
GURU NANAK KHALSA COLLEGE
92
scanner.close();
}
10
OUTPUT:-
Enter a character: A
A is an uppercase letter.
GURU NANAK KHALSA COLLEGE
92
Enter a character: b
b is a lowercase letter.
11
GURU NANAK KHALSA COLLEGE
92
import java.util.Scanner;
// Calculate average
average = (double) sum / numStudents;
// Output results
System.out.println("Sum of marks: " + sum);
System.out.println("Average marks: " + average);
scanner.close();
}
}
12
GURU NANAK KHALSA COLLEGE
92
OUTPUT:-
import java.util.Scanner;
13
}
GURU NANAK KHALSA COLLEGE
92
System.out.println("Minimum value="+a[0]+"\nMaximum
value:"+a[n-1]);
}
}
OUTPUT:-
Enter elements of array a:
2
3
4
Array elements are :
2 3 4 Minimum value=2nMaximum value:4
Minimum value value=2nMaximum value:4
Minimum value value=2nMaximum value:4
PS C:\Gargi>
import java.util.Scanner;
14
public static void main(String args[]) {
int n ;
String temp;
Scanner sc = new Scanner(System.in);
System.out.print("Enter no of names you want to enter: ");
n= sc.nextInt();
String a[]= new String[n];
System.out.print("Enter"+n+"names: ");
for(int i=0 ; i<n ; i++)
{
a[i]=sc.next();
}
System.out.println("array elements are:");
for(int i=0 ; i<n ; i++)
{
System.out.println(a[i] +"\t");
}
for(int i=0 ;i<n ; i++)
{
for(int j=i+1 ; j<n ; j++)
{
if(a[i].compareTo(a[j])>0)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
}
GURU NANAK KHALSA COLLEGE
92
15
OUTPUT:-
import java.util.Scanner;
scanner.close();
}
return transpose;
}
GURU NANAK KHALSA COLLEGE
92
17
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}
OUTPUT:-
Original Matrix:
897
987
087
Transpose of Matrix:
890
988
777
GURU NANAK KHALSA COLLEGE
92
18
Q5) Write a java program to find the addition of two matrices.Accept
matrix from user.
import java.util.Scanner;
19
printMatrix(sum);
scanner.close();
}
return sum;
GURU NANAK KHALSA COLLEGE
92
20
for (int j = 0; j < columns; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}
OUTPUT:-
First Matrix:
67
89
65
Second Matrix:
96
50
GURU NANAK KHALSA COLLEGE
92
98
21
GURU NANAK KHALSA COLLEGE
92
Aim:- Write the JAVA programs that illustrate the concepts: Class, Constructor
Overloading.
INPUT:-
import java.util.Scanner;
class Arithmetic
{
public String add(double i,double j)
{
return("Addition of "+i+" and "+j+" is"+(i+j));
}
public String sub(double i,double j)
{
return ("Subtraction of "+i+" and "+j+" is" +(i-j));
}
public String mul(double i,double j)
{
return("Multiplication of "+i+" and "+j+" is "+ (i*j));
}
public String div(double i,double j)
{
return("Division of "+i+" and "+j+" is "+(i/j));
}
}
public class Calc
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter no. 1");
22
double i=sc.nextDouble();
GURU NANAK KHALSA COLLEGE
92
OUTPUT:-
Enter no. 1
53
Enter no. 2
50
Addition of 53.0 and 50.0 is103.0
Subtraction of 53.0 and 50.0 is3.0
Multiplication of 53.0 and 50.0 is 2650.0
Division of 53.0 and 50.0 is 1.06
Q2) Write a java program to design a class Area for calculating area of
rectangle. Use the concept of constructor(default and parameterized
constructor).
INPUT:-
import java.util.Scanner;
class Area
{
double l,b;
Area()
{
l=25.3;
b=67;
23
GURU NANAK KHALSA COLLEGE
92
}
Area(double l,double b)
{
this.l=l;
this.b=b;
}
public String calArea()
{
return("Area of rect with len="+l+" and Breadth="+b+" is"+(l*b));
}
}
class TestArea
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter length:");
double i=sc.nextDouble();
System.out.println("Enter breadth:");
double j=sc.nextDouble();
Area a=new Area();
System.out.println("Default Constructor: "+a.calArea());
a=new Area(i,j);
System.out.println("Parameterized Constructor: "+a.calArea());
}
}
OUTPUT:-
Enter length:
10
Enter breadth:
5
Default Constructor: Area of rect with len=25.3 and Breadth=67.0
is1695.1000000000001
Parameterized Constructor: Area of rect with len=10.0 and Breadth=5.0
is50.0
GURU NANAK KHALSA COLLEGE
92
24
Q3) Create a class Vehicle with attributes color, speed and size. Create a subclass
of it say Car with special attributes such as cc and gears. Create a VehicleCheck
class to test its working.
INPUT:-
class Vehicle
{
String color;
int speed;
int size;
Vehicle(String c,int s,int sz)
{
color=c;
speed=s;
size=sz;
}
void printattributes()
{
System.out.println("Color:"+color);
System.out.println("Speed:"+speed);
System.out.println("Size:"+size);
}
}
//Car.java
class Car extends Vehicle
{
int CC;
int gears;
Car(String c,int s,int cc,int g)
{
super(c,s,sz);
CC=cc;
gears=g;
}
void Carattribute()
{
GURU NANAK KHALSA COLLEGE
92
System.out.println("NO of cc"+CC);
25
System.out.println("NO of gears"+gears);
}
}
//VehicleCheck.java
public class VehicleCheck
{
public static void main(String args[])
{
Car c= new Car("blue,200,20,150,5");
c.printattributes();
c.carattribute();
}
}
OUTPUT:-
Color:blue
Speed:200
Size:20
NO of cc150
NO of gears5
Q4) Create a class Animal with few methods in it. Create its child classes such as
Dog and Cat which overrides methods of its parent class. reate an AnimalWorld
class test working program.
INPUT:-
//Animal.java
class Animal
{
public Animal()
{
System.out.println("New animal has been created");
}
GURU NANAK KHALSA COLLEGE
92
26
public void sleep()
{
System.out.println("Animal Sleeps.....");
}
public void eat()
{
System.out.println("Animal is eating.....");
}
}
//Cat.java
class Cat extends Animal
{
public Cat()
{
super();
System.out.println("Cat class is Created");
}
public void sleep()
{
System.out.println("Cat is Sleeping.....");
}
public void meaw()
{
System.out.println("Cat meaws.....");
}
}
//Dog.java
class Dog extends Animal
{
public Dog()
{
super();
System.out.println("Dog class is Created");
}
public void Sleep()
{
System.out.println("Dog is Sleeping.....");
}
GURU NANAK KHALSA COLLEGE
92
27
System.out.println("Dog is Barking.....");
}
}
//AnimalWorld.java
public class Animalworld
{
public static void main(String[] args)
{
Cat c=new Cat();
Dog d=new Dog();
c.meaw();
c.eat();
d.bark();
d.eat();
}
}
OUTPUT:-
New animal has been created
Cat class is Created
New animal has been
created Dog class is
Created Cat meaws.....
Animal is eating.....
Dog is Barking.....
Animal is eating.....
GURU NANAK KHALSA COLLEGE
92
CODE:-
// Animal.java
abstract class Animal {
public abstract void sound();
}
// Lion.java
class Lion extends Animal {
public void sound() {
System.out.println("Lion roars!");
}
}
// Tiger.java
class Tiger extends Animal {
public void sound() {
System.out.println("Tiger growls");
}
}
// Test.java
public class Test1 {
public static void main(String[] args) {
Animal lion = new Lion();
lion.sound();
Tiger tiger = new Tiger();
tiger.sound();
}
}
OUTPUT:-
Lion roars!
29
Tiger growls
GURU NANAK KHALSA COLLEGE
92
Q2)Write a Java program to create an abstract class BankAccount with abstract methods
deposit() and withdraw(). Create a subclass SavingsAccount that extend the
BankAccount class and implement the respective methods to handle deposits and
withdrawals for each account type.
CODE:-
// BankAccount.java
abstract class BankAccount {
private int accountNumber;
private double balance;
}
public double getBalance() {
return balance;
}
protected void setBalance(double b) {
balance = b;
}
public abstract void deposit(double amount);
public abstract void withdraw(double amount);
}
// SavingsAccount.java
class SavingsAccount extends BankAccount {
30
}
GURU NANAK KHALSA COLLEGE
92
}
}
}
// Main.java
public class Main1{
public static void main(String[] args) {
SavingsAccount sa = new SavingsAccount(1, 1000);
sa.deposit(2000);
sa.withdraw(500);
sa.withdraw(2000);
}
}
OUTPUT:-
31
Q3) Create an interface name like Square, Rectangle, Circle which defines
incomplete methods such as calArea and calCircum. Create class Shapes
which uses all interface for implementing unimplemented method.
CODE:-
interface Circle
{
void area_circle(int r);
void circum_circle(int r);
}
interface Rectangele
{
void area_rect(int l,int b);
void circum_rect(int l,int b);
} interface Square
{
void area_square(int a);
void circum_rect(int l,int b);
} class Shapes implements Circle, Square, Rectangele
{
public void area_circle(int r)
{
double area=Math.PI*Math.sqrt(r);
System.out.println("Area of circle : "+area);
}
public void circum_circle(int r)
{
double circum=6.28*r;
System.out.println("Circumference of circle:"+circum);
}
public void area_square(int a)
{
int area=a*a;
System.out.println("area of square:"+area);
}
GURU NANAK KHALSA COLLEGE
92
32
public void circum_square(int a)
{
int circum=4*a;
System.out.println("Circumference of square:"+circum);
}
public void circum_rect(int l, int b)
{
int circum=2*(l+b);
System.out.println("Circumference of rectangle:"+circum);
}
public void area_rect(int l, int b)
{
int area=l*b;
System.out.println("Area of rectangle:"+area);
}
}
class Shapetest
{
public static void main(String args[])
{
Shapes sh=new Shapes();
sh.area_circle(3);
sh.circum_square(3);
sh.area_square(4);
sh.circum_square(4);
sh.area_rect(10,20);
sh.circum_rect(10,20);
}
}
GURU NANAK KHALSA COLLEGE
92
OUTPUT:-
33
Area of circle : 5.441398092702653
Circumference of square:12
area of square:16
Circumference of square:16
Area of rectangle:200
Circumference of rectangle:60
GURU NANAK KHALSA COLLEGE
92
Q1) Write a program which takes name and age from the user on click of the
button and display a message on label, user is eligible to vote or not.
35
GURU NANAK KHALSA COLLEGE
92
36
GURU NANAK KHALSA COLLEGE
92
37
GURU NANAK KHALSA COLLEGE
92
Q2) Write a program to create two textfield and four radio buttons
(+,-,*,%)and on selecting the radiobutton the operation should be performed
and the result should be displayed in JOptionPane.
38
GURU NANAK KHALSA COLLEGE
92
input();
JOptionPane.showMessageDialog(rdbtnMul, “Multiplication is “+(n1*n2));
input();
JOptionPane.showMessageDialog(rdbtnDiv, “Division is “+(n1/n2)); 39
GURU NANAK KHALSA COLLEGE
92
40
GURU NANAK KHALSA COLLEGE
92
41
GURU NANAK KHALSA COLLEGE
92
42
GURU NANAK KHALSA COLLEGE
92
43
GURU NANAK KHALSA COLLEGE
92
44
GURU NANAK KHALSA COLLEGE
92
Q5)Create an application where user can place order for pizza. Accept
user-name, address, mobile- no from user. Give options for 4 types of pizza
(basic, thick & chewy, thin & crispy, Chicago deep dish). Also provide options
for multiple toppings (Pepperoni, sausage, black olives, and mushrooms).
Confirm the order by displaying all the details in a JOptionPane.
46
GURU NANAK KHALSA COLLEGE
92
47
GURU NANAK KHALSA COLLEGE
92
48
GURU NANAK KHALSA COLLEGE
92
49
50
GURU NANAK KHALSA COLLEGE
92
A)import java.io.*;
import java.net.*;
import java.util.Scanner;
class ClientDemoNo
{
public static void main(String a[]) throws IOException
{
Socket s=new Socket("127.0.0.1",5678);
System.out.println("enter any no:");
Scanner sc=new Scanner(System.in);
String no=sc.nextLine();
PrintStream ps=new PrintStream(s.getOutputStream());
ps.println(no);
Scanner sc1=new Scanner (s.getInputStream());
String msg=sc1.nextLine();
System.out.println(msg);
ps.flush();
s.close();
}
}
B)import java.util.Scanner;
import java.net.*;
import java.io.*;
class ServerDemoNo
{
public static void main(String a[]) throws IOException
{
Socket s;
ServerSocket ss=new ServerSocket(5678);
while(true)
{
s=ss.accept();
System.out.println("connection Established");
Scanner sc=new Scanner (s.getInputStream());
String msg=sc.nextLine();
int n=Integer.parseInt(msg);
PrintStream ps=new PrintStream(s.getOutputStream());
String st;
if(n<0)
{
st=n+" is -ve";
ps.println(st);
GURU NANAK KHALSA COLLEGE
92
}
else if(n>0)
{
st=n+" is +ve";
ps.println(st);
}
else
ps.println("zero");
s.close();
}}}
OUTPUT
on first cmd
on second cmd
GURU NANAK KHALSA COLLEGE
92
53