Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
33 views

Java LAB

The document contains 10 Java programming questions and their solutions. It includes programs to solve quadratic equations, multiply arrays, demonstrate bitwise operators, sort arrays, create student objects, demonstrate method and constructor overloading, create staff subclasses, demonstrate dynamic dispatch and exception handling.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Java LAB

The document contains 10 Java programming questions and their solutions. It includes programs to solve quadratic equations, multiply arrays, demonstrate bitwise operators, sort arrays, create student objects, demonstrate method and constructor overloading, create staff subclasses, demonstrate dynamic dispatch and exception handling.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Questions:

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;

public class Quadratic


{
public static void main(String[] args)
{

double root1, root2,a,b,c,determinant,r;


Scanner input = new Scanner(System.in);

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 {

public static void main(String[] args) {


int a1[] = {2,5,-2,10};
int a2[] = {3,-5,7,1};

for (int i = 0; i < a1.length; i++) {


a1[i] = a1[i] * a2[i];
}

for (int i = 0; i < a1.length; i++)


{
System.out.print(a1[i] + " ");
}
}
}

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;

public class ArraySortingExample {


public static void main(String[] args) {
Scanner ed = new Scanner(System.in);
int[] a = new int[50];
int i, j, temp;
System.out.println("Please Enter 5 elements in the Array");
for (i = 0; i < 5; i++) {
a[i] = ed.nextInt();
}
for (i = 0; i < 5; i++) {
for (j = i + 1; j < 5; j++) {
if (a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.println("Sorted Array in Increasing Order:-");
for (j = 0; j < 5; j++) {
System.out.println(a[j]);
}
for (i = 0; i < 5; i++) {
for (j = i + 1; j < 5; j++) {
if (a[i] < a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.println("Sorted Array in Decreasing Order:-");
for (j = 0; j < 5; j++) {
System.out.println(a[j]);
}
}
}

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();
}

public void display() {


System.out.println(usn + "\t" + name + "\t" + branch + "\t" + phone + "\t" + percentage);
}
}
public class Studentinfo {
public static void main(String[] args) {
System.out.println("Enter the total number of students");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();

// 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");
}
}

public class Mco {

public static void main(String[] args) {


ABC ob1 = new ABC(20, 20);
ABC ob2 = new ABC(20);
ob1.meth();
ob1.meth(40);
}
}

7)
package staffmain;
import java.util.Scanner;
class Staff {
private String staffId;
private String name;
private double phone;
private float salary;

public void accept() {


Scanner scanner = new Scanner(System.in);
System.out.println("Enter Staff Id");
staffId = scanner.next();
System.out.println("Enter Name");
name = scanner.next();
System.out.println("Enter Phone");
phone = scanner.nextInt();
System.out.println("Enter Salary");
salary = scanner.nextFloat();
}
public void display() {
System.out.println("Staff Id: " + staffId);
System.out.println("Name: " + name);
System.out.println("Phone: " + phone);
System.out.println("Salary: " + salary);
}
}
class Teaching extends Staff {
private String domain;
private String[] publications;
public void accept() {
super.accept();
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Domain");
domain = scanner.next();
System.out.println("Enter Number of Publications");
int n = scanner.nextInt();
publications = new String[n];
System.out.println("Enter Publications");
for (int i = 0; i < n; i++) {
publications[i] = scanner.next();
}
}

public void display() {


super.display();
System.out.println("Domain: " + domain);
System.out.println("Publications:");
for (int i = 0; i < publications.length; i++)
{
System.out.println(publications[i]);
}
}
}
class Technical extends Staff {
private String[] skills;

public void accept() {


super.accept();
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Number of Skills");
int n = scanner.nextInt();
skills = new String[n];
System.out.println("Enter Skills");
for (int i = 0; i < n; i++) {
skills[i] = scanner.next();
}
}

public void display() {


super.display();
System.out.println("Skills:");
for (int i = 0; i < skills.length; i++) {
System.out.println(skills[i]);
}
}
}

class Contract extends Staff {


private int period;
public void accept() {
super.accept();
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Period");
period = scanner.nextInt();
}

public void display() {


super.display();
System.out.println("Period: " + period);
}
}

public class Staffmain {

public static void main(String[] args) {


Teaching teaching = new Teaching();
Technical technical = new Technical();
Contract contract = new Contract();
System.out.println("Enter Details for Teaching Staff Member");
teaching.accept();
System.out.println("Enter Details for Technical Staff Member");
technical.accept();
System.out.println("Enter Details for Contract Staff Member");
contract.accept();
System.out.println("Details for Teaching Staff Member are");
teaching.display();
System.out.println("Details for Technical Staff Member are");
technical.display();
System.out.println("Details for Contract Staff Member are");
contract.display();
}
}

8)
package dynamicdsp;

abstract class Figure {


double d1;
double d2;
Figure(double a, double b) {
d1 = a;
d2 = b;
}

abstract double area();


}
class Rectangle extends Figure {
Rectangle(double a, double b) {
super(a, b);
}

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 {

public static void main(String[] args) {


Rectangle r = new Rectangle(5, 10);
Triangle t = new Triangle(5, 10);
Figure f; //reference only(no object is created)

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 D extends A {


public void displayD(){
a=100;
System.out.println("Class D: "+"The value of a is "+a);
}
}

public class E {
private void displayE(){
System.out.println("Class E");
}
}

package packagesdemo;

import P1.*;
import P2.*;
public class Packagesdemo {

public static void main(String[] args) {


B b=new B();
C c=new C();
D d=new D();
E e=new E();

b.displayB(); //public method, protected data member


//error c.displayC(); //default method
d.displayD(); //public method,protected data member
//error e.displayE(); //private method

}
}
10)

(WRITE THESE IN LOER CASE LETTERS)

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.");
}

You might also like