0% found this document useful (0 votes)
9 views24 pages

Programs

Uploaded by

Megha Kulkarni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views24 pages

Programs

Uploaded by

Megha Kulkarni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Basics of Java Programming BPLCK205C

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.

public class Main {


public static void main(String[] args) {

// value a, b, and c
double a = 4.5, b = 5, c = 3.4 ,root1, root2;

// calculate the determinant (b2 - 4ac)


double determinant = b * b - 4 * a * c;

// check if determinant is greater than 0


if (determinant > 0) {

// two real and distinct roots


root1 = (-b + Math.sqrt(determinant)) / (2 * a);
root2 = (-b - Math.sqrt(determinant)) / (2 * a);

System.out.printf("root1 = %.2f and root2 = %.2f", root1, root2);


}
// check if determinant is equal to 0
else if (determinant == 0) {

// two real and equal roots


// determinant is equal to 0
// so -b + 0 == -b
root1 = root2 = -b / (2 * a);
System.out.printf("root1 = root2 = %.2f;", root1);
}

// if determinant is less than zero


else {
// roots are complex number and distinct
double real = -b / (2 * a);
double imaginary = Math.sqrt(-determinant) / (2 * a);
System.out.printf("root1 = %.2f+%.2fi", real, imaginary);
System.out.printf("\nroot2 = %.2f-%.2fi", real, imaginary);
}
}
}

Dept. of CSE, VTU, Belagavi 1


Basics of Java Programming BPLCK205C
Output:-
root1 = -0.56+0.67i
root2 = -0.56-0.67i

Dept. of CSE, VTU, Belagavi 2


Basics of Java Programming BPLCK205C

2. Write a JAVA program for multiplication of two arrays.

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

int a1[ ] = { 4, 6, -4, 8 };


int a2[ ] = { 4, -8, 6, 3 };
int len=4;
int result[ ] = new int[len];
for (int i = 0; i<len; i++)
{

// multiplying corresponding element


result[i] = a1[i] * a2[i];
System.out.print(result[i] + " ");
}
}
}
Output:-
16 -48 -24 24

Dept. of CSE, VTU, Belagavi 3


Basics of Java Programming BPLCK205C

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 ;

// shifting the value by 4 using left shift operator


int y = x <<4 ;

// printing the left shifted value


System.out.println( " The leftshift value is: " +y) ;
x = -80 ;

// shifting the value by 4 using right shift operator


y = x >>4 ;

// printing the right shifted value


System.out.println( " The right shifted value is : " +y) ;
x = 80 ;

// shifting the value by 4 using unsigned right shift operator


y = x >>>4 ;

// printing the unsigned right shifted value


System.out.println( " The right shifted value is : " +y) ;

}
}

Output:-
The leftshift value is: 80
The right shifted value is : -5
The right shifted value is : 5

Dept. of CSE, VTU, Belagavi 4


Basics of Java Programming BPLCK205C

Dept. of CSE, VTU, Belagavi 5


Basics of Java Programming BPLCK205C

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

System.out.print("SORTED LIST IN ASCENDING ORDER :- ");


for(i=0;i<len;i++)
System.out.print(a[i]+",");
System.out.print("\nSORTED LIST IN DESCENDING ORDER :- ");
for(i=len-1;i>=0;i--)
System.out.print(a[i]+",");
}
}

Output:-
SORTED LIST IN ASCENDING ORDER :- -68, -2, 5, 8, 90
SORTED LIST IN DESCENDING ORDER :- 90 8 5, -2, -68

Dept. of CSE, VTU, Belagavi 6


Basics of Java Programming BPLCK205C

Dept. of CSE, VTU, Belagavi 7


Basics of Java Programming BPLCK205C

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;

public void accept()


{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Usn:");
usn = scanner.nextLine();
System.out.println("Enter Name:");
name = scanner.nextLine();
System.out.println("Enter Branch:");
branch = scanner.nextLine();
System.out.println("Enter Phone:");
ph = scanner.nextLine();
System.out.println("Enter Percentage:");
percentage = scanner.nextInt();
}

public void display()


{
System.out.println("Usn: " + usn);
System.out.println("Name: " + name);
System.out.println("Branch: " + branch);
System.out.println("Phone: " + ph);
System.out.println("Percentage: " + percentage);
System.out.println();
}
}

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

Dept. of CSE, VTU, Belagavi 8


Basics of Java Programming BPLCK205C
Scanner in = new Scanner(System.in);
System.out.println("Enter total number of students:");
int n = in.nextInt();
Student[] ob = new Student[n];
for (int i = 0; i< n; i++)
{
System.out.println("\nEnter details for student "+(i+1)+":-");
ob[i] = new Student();
ob[i].accept();
}
for (int i = 0; i< n; i++)
{
System.out.println("\nDetails of student "+(i+1)+":-");
ob[i].display();
}
}
}

Output:-
Enter total number of students:
2

Enter details for student 1:-


Enter Usn:
2VX23CB028
Enter Name:
Manoj Kumar V
Enter Branch:
CSBS
Enter Phone:
7975464020
Enter Percentage:
85.56

Enter details for student 2:-


Enter Usn:
4VV22IS025
Enter Name:
Praveen Kumar L
Enter Branch:
ISE
Enter Phone:
8792738258
Enter Percentage:
90.16

Dept. of CSE, VTU, Belagavi 9


Basics of Java Programming BPLCK205C
Details of student 1:-
Usn: 2VX23CB028
Name: Manoj Kumar V
Branch: CSBS
Phone: 7975464020
Percentage: 85.56

Details of student 2:-


Usn: 4VV22IS025
Name: Praveen Kumar L
Branch: ISE
Phone: 8792738258
Percentage: 90.16

Dept. of CSE, VTU, Belagavi 10


Basics of Java Programming BPLCK205C

6. Write a JAVA program demonstrating Method overloading and Constructor overloading.

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

int area(int a, int b)


{
int res=a*b;
return res;
}
}
class lab6
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter values for 2 sides");
int value1=sc.nextInt();
int value2=sc.nextInt();
Overload ob1=new Overload(value1);
System.out.println("Area of square="+ob1.area());
System.out.println("Area of rectangle="+ob1.area(value1,value2));
Overload ob2=new Overload();
System.out.println("Area of square="+ob2.area());
}
}

Dept. of CSE, VTU, Belagavi 11


Basics of Java Programming BPLCK205C

Output:-
Enter values for 2 sides
4
5
Area of square=16
Area of rectangle=20
Area of square=100

Dept. of CSE, VTU, Belagavi 12


Basics of Java Programming BPLCK205C

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;

Dept. of CSE, VTU, Belagavi 13


Basics of Java Programming BPLCK205C
}
public void Display()
{
super.Display();
System.out.print("\t\t--"+"\t\t"+"--"+"\t"+Skills+"\t"+"--");
}
}
class Contract extends Staff
{
private int Period;
public Contract(int id, String n, String ph, long sal, int per)
{
super(id, n, ph, sal);
Period = per;
}
public void Display()
{
super.Display();
System.out.print("\t\t--"+"\t\t"+"--"+"\t\t"+"--"+"\t"+Period);
}
}
class lab7
{
public static void main(String[] args)
{
Staff st[]=new Staff[3];
st[0]=new Teaching(0001,"Manoj","271173",90000,"CSE",3);
st[1]=new Technical(0002,"Praveen","271172",2000,"Server Admin");
st[2]=new Contract(0003,"Mythri","271174",9000,3);
System.out.println("Staff ID\tName\t\tPhone\t\tSalary\t\tDomain\tPublication\tSkills\t\tPeriod");
for(int i=0;i<3;i++)
{
st[i].Display();
System.out.println();
}
}
}

Dept. of CSE, VTU, Belagavi 14


Basics of Java Programming BPLCK205C
Output:-
Staff ID Name Phone Salary Domain Publication Skills Period
1 Manoj 271173 40000 CSE 3 -- --
2 Praveen 271172 2000 -- -- Server Admin --
3 Mythri 271174 9000 -- -- -- 3

Dept. of CSE, VTU, Belagavi 15


Basics of Java Programming BPLCK205C

8. Demonstrate dynamic dispatch using abstract class in JAVA.

abstract class Shape


{
abstract void draw();
}
class Circle extends Shape
{
void draw()
{
System.out.println("Drawing Circle");
}
}
class Square extends Shape
{
void draw()
{
System.out.println("Drawing Square");
}
}
class lab8
{
public static void main(String[] args)
{
Shape s;
s = new Circle();
s.draw();
s = new Square();
s.draw();
}
}

Output:-
Drawing Circle
Drawing Square

Dept. of CSE, VTU, Belagavi 16


Basics of Java Programming BPLCK205C

Dept. of CSE, VTU, Belagavi 17


Basics of Java Programming BPLCK205C

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.

Steps to execute this program:


 First create a folder com in the same folder where you saved lab9.java
 Then within com directory create two directories P1 and P2. (note P is in uppercase)
 Within P1 directory store the files
A.java,
B.java
C.java.
 Within P2 directory store the files
D.java
E.java
 Now go to terminal
 Compile using the statement: javac lab9.java
 Execute using the statement: java lab9

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

Dept. of CSE, VTU, Belagavi 18


Basics of Java Programming BPLCK205C
B.java
// Package P1
package com.P1;
// Class B inherited from A with default variable and public method
public class B extends A
{
int z;
public void setZ(int z)
{
this.z = z;
}
public int getZ()
{
return z;
}
}

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

Dept. of CSE, VTU, Belagavi 19


Basics of Java Programming BPLCK205C
public int getV()
{
return 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());

Dept. of CSE, VTU, Belagavi 20


Basics of Java Programming BPLCK205C
System.out.println("Value of z: " + b.getZ());
// Creating an object of class C in package P1
C c = new C();
c.setW(60);
System.out.println("Value of w: " + c.getW());
// Creating an object of class D in package P2
D d = new D();
d.setX(70);
d.setY(80);
d.setV(90);
System.out.println("Value of x: " + d.getX());
System.out.println("Value of y: " + d.getY());
System.out.println("Value of v: " + d.getV());
// Creating an object of class E in package P2
E e = new E();
e.setU(100);
System.out.println("Value of u: " + e.getU());
}
}

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

Dept. of CSE, VTU, Belagavi 21


Basics of Java Programming BPLCK205C

Dept. of CSE, VTU, Belagavi 22


Basics of Java Programming BPLCK205C

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

Dept. of CSE, VTU, Belagavi 23


Basics of Java Programming BPLCK205C

Dept. of CSE, VTU, Belagavi 24

You might also like