First Year Java Lab
First Year Java Lab
Compilation Command
F:\Java>javac FirstExample3.java
Run Command
F:\Java>java FirstExample3
This is a simple Java program for First year Students.
class Example2
{
public static void main(String args[])
{
int num; // this declares a variable called num
num = 100; // this assigns num the value 100
System.out.println("This is num: " + num);
num = num * 2;
System.out.print("The value of num * 2 is ");
System.out.println(num);
}
}
Result:
This is num: 100
The value of num * 2 is 200
//Exp1: Quadratic Equation
//Question 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.
import java.util.Scanner;
public class QuadraticEquationDemo
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter a: ");
double a = input.nextDouble();
System.out.print("Enter b: ");
double b = input.nextDouble();
System.out.print("Enter c: ");
double c = input.nextDouble();
double discriminant = b * b - 4 * a * c;
if (discriminant > 0)
{
System.out.println("The equation has real and distinct roots.");
double root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
double root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
System.out.println("The roots are " + root1 + " and " + root2);
}
else if (discriminant == 0)
{
System.out.println("The equation has real and equal roots.");
double root = -b / (2 * a);
System.out.println("The root is " + root);
}
else
{
System.out.println("The equation has no real roots.");
double realp = -b / (2 * a);
double imagp = (Math.sqrt(-discriminant)) / (2 * a);
System.out.printf("The roots are (%.2f + i%.4f) and (%.2f - i%.4f)\n",realp, imagp, realp, imagp);
}
Result:
F:\Java>java QuadraticEquationDemo
Enter a: 2
Enter b: 3
Enter c: 4
The equation has no real roots.
The roots are (-0.75 + i1.1990) and (-0.75 - i1.1990)
//Exp1: Array Multiplication
//Question Write a JAVA program for multiplication of two arrays.
import java.util.Scanner;
import java.util.Arrays;
if (n != m)
{
System.out.println("Error: Arrays have different length, not possible to multiply them.");
return;
}
F:\Java>java MultiplyArraysDemo
Enter the number of elements in the first array: 2
Enter element 1 of the first array: 1
Enter element 2 of the first array: 2
Enter the number of elements in the second array: 2
Enter element 1 of the second array: 3
Enter element 2 of the second array: 4
Result of multiplying arrays: [3, 8]
Exp3:Question Demonstrate the following operations and sign extension with Java programs
i) ≪
ii) ≫
iii) ⋙
import java.util.Scanner;
class OperatorDemoSignExtension {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
16 << 2 = 64
16 >> 2 = 4
}
}
Result:
F:\Java>javac ExchangeSortDemo1.java
F:\Java>java ExchangeSortDemo1
Enter the number of elements in the array:
4
Enter the elements of the array:
6
1
90
43
class StudentType
{
private String USN;
private String NAME;
private String BRANCH;
private String PHONE;
private double PERCENTAGE;
public StudentType(String USN, String NAME, String BRANCH, String PHONE, double
PERCENTAGE)
{
this.USN = USN;
this.NAME = NAME;
this.BRANCH = BRANCH;
this.PHONE = PHONE;
this.PERCENTAGE = PERCENTAGE;
public String getUSN()
{
return USN;
}
STUDENT DETAILS
=====================
USN NAME BRANCH PHONE PERCENTAGE
tyr456 jag ec 7685468 49.0
iuyrefhj7 jkodr cse 908654 67.0
9jhgfrtys kloitr ise 098765 9.0
Ex6: Polymorphism - Method Overloading: Write a JAVA program demonstrating
Method overloading and Constructor overloading.
class PointType
{
private double x;
private double y;
// Constructor overloading
public PointType()
{
this.x = 0;
this.y = 0;
}
public Technical(int staffId, String name, String phone, double salary, String skills)
{
super(staffId, name, phone, salary);
this.skills = skills;
}
public Contract(int staffId, String name, String phone, double salary, int period)
{
super(staffId, name, phone, salary);
this.period = period;
}
class DynamicDispatchDemo
{
public static void main(String[] args)
{
Shape s;
s = new Circle();
s.draw();
s = new Square();
s.draw();
Result:
F:\Java>java DynamicDispatchDemo
Drawing Circle
Drawing Square
Experiment9: 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.
// Package P1
package com.P1;
// Class A1 with private and protected variables and public methods
public class A
{
private int x;
protected int y;
// Save as C.java
// Package P2
package com.P2;