Java LAB
Java LAB
1 Write a JAVA program that prints all real solutions to the quadratic equation ax2+bx+c=0.
Read in a, b, c and use the quadratic formula.
2 Write a JAVA program for multiplication of two arrays.
3 Demonstrate the following operations and sign extension with Java programs
(i) << (ii) >> (iii) >>>
4 Write a JAVA program to sort list of elements in ascending and descending order
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.
6 Write a JAVA program demonstrating Method overloading and Constructor overloading.
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.
8 Demonstrate dynamic dispatch using abstract class in JAVA.
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.
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.
Solutions:
1)
package quadratic;
import java.util.Scanner;
System.out.print("Input a: ");
a = input.nextDouble();
System.out.print("Input b: ");
b = input.nextDouble();
System.out.print("Input c: ");
c = input.nextDouble();
determinant = b * b - 4 * a * c;
r=Math.sqrt(determinant);
if (determinant > 0)
{
root1 = (-b+r)/(2*a);
root2 = (-b-r)/(2*a);
System.out.format("root1 = %.2f and root2 = %.2f\n", root1, root2);
}
else if (determinant == 0)
{
root1 = root2 = (-b)/(2*a);
System.out.format("Roots are equal\n");
System.out.format("root1 = root2 = %.2f\n;", root1);
}
else
{
// roots are complex number and distinct
double real = -b / (2 * a);
double imaginary = Math.sqrt(-determinant) / (2 * a);
System.out.format("Roots are imaginary\n");
System.out.format("root1 = %.2f+%.2fi\n", real, imaginary);
System.out.format("\nroot2 = %.2f-%.2fi\n", real, imaginary);
}
}
}
2)
package multiplyarray;
import java.util.Arrays;
public class Multiplyarray {
3)
package shiftop;
public class Shiftop
{
public static void main(String args[]) {
byte x, y;
x = 10;
y = -10;
System.out.println("Bitwise Left Shift: x<<2 = " + (x << 2));
System.out.println("Bitwise Right Shift: x>>2 = " + (x >> 2));
System.out.println("Bitwise Zero Fill Right Shift: x>>>2 = " + (x >>> 2));
System.out.println("Bitwise Zero Fill Right Shift: y>>>2 = " + (y >>> 2));
}
}
4)
import java.util.Scanner;
5)
package studentinfo;
import java.util.Scanner;
class Student {
private String usn;
private String name;
private String branch;
private String phone;
private String percentage;
public void read() {
Scanner sc = new Scanner(System.in);
usn = sc.nextLine();
name = sc.nextLine();
branch = sc.nextLine();
phone = sc.nextLine();
percentage = sc.nextLine();
}
// Create n objects
Student[] st = new Student[n];
for (int i = 0; i < st.length; i++) {
st[i] = new Student(); }
for (int i = 0; i < n; i++) { System.out.println("Enter USN, Name, Branch, percentage of
marks and Phone no. for
student " + (i + 1));
st[i].read();
}
// Display the student information
System.out.println("USN\tName\tBranch\tPercentage\tPhone");
for (int i = 0; i < n; i++) {
st[i].display();
}
}
}
6)
package mco;
class ABC {
int a, b;
ABC(int x, int y) {
a = x;
b = y;
System.out.println(" First version of constructor is called");
System.out.println("values of a and b are\t" + a + " " + b);
}
ABC(int x) {
a = x;
b = 10;
System.out.println(" Second version of constructor is called");
System.out.println("values of a and b are\t" + a + " " + b);
}
void meth() {
System.out.println(" First version of method is called");
}
void meth(int x) {
System.out.println(" Second version of method is called");
}
}
7)
package staffmain;
import java.util.Scanner;
class Staff {
private String staffId;
private String name;
private double phone;
private float salary;
8)
package dynamicdsp;
double area() {
System.out.println("Inside Area for Rectangle.");
return d1 * d2;
}
}
class Triangle extends Figure {
Triangle(double a, double b) {
super(a, b);
}
double area() {
System.out.println("Inside Area for Triangle.");
return 0.5 * d1 * d2;
}
}
public class Dynamicdsp {
f = r;
System.out.println("Area is " + f.area());
f = t;
System.out.println("Area is " + f.area());
}
}
9)
package P1;
public class A {
protected int a;
void displayA(){
System.out.println("class A");
}
}
public class B extends A{
public void displayB(){
a=10;
System.out.println("Class B:"+" The value of a is "+a);
}
}
public class C {
void displayC(){
System.out.println("Class C");
}
}
package P2;
import P1.*;
public class E {
private void displayE(){
System.out.println("Class E");
}
}
package packagesdemo;
import P1.*;
import P2.*;
public class Packagesdemo {
}
}
10)
package excdemo;
import java.util.scanner;
public class excdemo {
PUBLIC STATIC VOID MAIN(STRING ARGS[]) {
INT A, B, RESULT;
SCANNER S = NEW SCANNER(SYSTEM.IN);
SYSTEM.OUT.PRINTLN("ENTER THE VALUE OF A AND B");
A = S.NEXTINT();
B = S.NEXTINT();
TRY {
RESULT = A / B;
INT C[] = {1};
C[40] = 99;
} CATCH (ARITHMETICEXCEPTION E) {
SYSTEM.OUT.PRINTLN("DIVIDE BY 0: " + E);
} CATCH (ARRAYINDEXOUTOFBOUNDSEXCEPTION E) {
SYSTEM.OUT.PRINTLN("ARRAY INDEX OUT OF BOUND: " + E);
}
SYSTEM.OUT.PRINTLN("AFTER TRY/CATCH BLOCKS.");
}