Programs
Programs
1. Write a JAVA program that prints all real solutions to the quadratic equation ax2+bx+c=0.
Readin a, b, c and use the quadratic formula.
// value a, b, and c
double a = 4.5, b = 5, c = 3.4 ,root1, root2;
class ArrayMul {
public static void main(String[] args)
{
3. Demonstrate the following operations and sign extension with Java programs
(i) << (ii) >> (iii) >>>
public class Op {
public static void main( String[ ] args )
{
int x = 5 ;
}
}
Output:-
The leftshift value is: 80
The right shifted value is : -5
The right shifted value is : 5
4. Write a JAVA program to sort list of elements in ascending and descending order.
class demo
{
public static void main(String args[])
{
int i,j,len=5;
int a[]={-2,-68,90,8,5};
for(i=0;i<len;i++)
{
for(j=i+1;j<len;j++)
{
if(a[i]>a[j])
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
Output:-
SORTED LIST IN ASCENDING ORDER :- -68, -2, 5, 8, 90
SORTED LIST IN DESCENDING ORDER :- 90 8 5, -2, -68
5. Create a JAVA class called Student with the following details as variables within it.
USN
NAME
BRANCH
PHONE
PERCENTAGE
Write a JAVA program to create n Student objects and print the USN, Name, Branch, Phone, and
percentage of these objects with suitable headings.
import java.util.Scanner;
class Student
{
private String usn, name, branch, branch;
double percentage;
class demo
{
public static void main(String[] args)
{
Output:-
Enter total number of students:
2
import java.util.*;
class Overload
{
int side1,side2;
Overload(int a)
{
side1=a;
}
Overload()
{
side1=10;
side2=20;
}
int area()
{
int res=side1*side1;
return res;
}
Output:-
Enter values for 2 sides
4
5
Area of square=16
Area of rectangle=20
Area of square=100
7. Design a super class 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.
class Staff
{
private int StaffId;
private String Name;
private String Phone;
private long Salary;
public Staff(int id,Stringn,Stringph,longsal)
{
StaffId = id;
Name = n;
Phone = ph;
Salary = sal;
}
public void Display()
{
System.out.print("\t"+StaffId+"\t"+Name+"\t\t"+Phone+"\t\t"+Salary);
}
}
class Teaching extends Staff
{
private String Domain;
private int Publications;
public Teaching(int id, String n, String ph,longsal, String domain, int pub)
{
super(id, n, ph, sal);
Domain = domain;
Publications = pub;
}
public void Display()
{
super.Display();
System.out.print("\t\t"+Domain+"\t\t"+Publications+"\t\t"+"--"+"\t"+"--");
}
}
class Technical extends Staff
{
private String Skills;
public Technical(int id, String n, String ph,longsal, String sk)
{
super(id, n, ph, sal);
Skills = sk;
Output:-
Drawing Circle
Drawing Square
9. Create two packages P1 and P2. In package P1, create class A, class B inherited from A, class C . In
package P2, create class D inherited from class A in package P1 and class E. Demonstrate working of
access modifiers (private, public, protected, default) in all these classes using JAVA.
A.java
// Package P1
package com.P1;
// Class A with private and protected variables and public methods
public class A
{
private int x;
protected int y;
public void setX(int x)
{
this.x = x;
}
public int getX()
{
return x;
}
public void setY(int y)
{
this.y = y;
}
public int getY()
{
return y;
}
}
C.java
// Package P1
package com.P1;
// Class C with private variable and public method
public class C
{
private int w;
public void setW(int w)
{
this.w = w;
}
public int getW()
{
return w;
}
}
D.java
// Package P2
package com.P2;
// Class D inherited from A with protected variable and public method
public class D extends com.P1.A
{
protected int v;
public void setV(int v)
{
this.v = v;
}
E.java
// Package P2
package com.P2;
// Class E with private variable and public method
public class E
{
private int u;
public void setU(int u)
{
this.u = u;
}
public int getU()
{
return u;
}
}
lab9.java
import com.P1.A;
import com.P1.B;
import com.P1.C;
import com.P2.D;
import com.P2.E;
public class lab9
{
public static void main(String[] args)
{
// Creating an object of class A in package P1
A a = new A();
a.setX(10);
a.setY(20);
System.out.println("Value of x: " + a.getX());
System.out.println("Value of y: " + a.getY());
// Creating an object of class B in package P1
B b = new B();
b.setX(30);
b.setY(40);
b.setZ(50);
System.out.println("Value of x: " + b.getX());
System.out.println("Value of y: " + b.getY());
Output:
Value of x: 10
Value of y: 20
Value of x: 30
Value of y: 40
Value of z: 50
Value of w: 60
Value of x: 70
Value of y: 80
Value of v: 90
Value of u: 100
10. 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. Also demonstrate working of
ArrayIndexOutOfBoundException.
import java.util.Scanner;
public class lab10
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter two integers: ");
int a = sc.nextInt();
int b = sc.nextInt();
try
{
if (b == 0)
{
throw new ArithmeticException("Division by zero");
}
int result = a / b;
System.out.println("Result of integer division: " + result);
}
catch (ArithmeticException e)
{
System.out.println(e.getMessage());
}
try
{
int[] arr = new int[5];
arr[10] = 50; //invalid index generates exception
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println(e.getMessage());
}
}
}
Output:-
Enter two integers:
45
5
Result of integer division: 9
Index 10 out of bounds for length 5