Javalab
Javalab
import java.io.*;
class GFG {
System.out.println();
}
}
return C;
}
// Driver code
public static void main(String[] args)
{
int size = 4;
int A[][] = { { 1, 1, 1, 1 },
{ 2, 2, 2, 2 },
{ 3, 3, 3, 3 },
{ 4, 4, 4, 4 } };
// Print the matrices A
System.out.println("\nMatrix A:");
printMatrix(A, size, size);
int B[][] = { { 1, 1, 1, 1 },
{ 2, 2, 2, 2 },
{ 3, 3, 3, 3 },
{ 4, 4, 4, 4 } };
// Print the matrices B
System.out.println("\nMatrix B:");
printMatrix(B, size, size);
import java.io.*;
import java.util.*;
class Test
{
// Pushing element on the top of the stack
static void stack_push(Stack<Integer> stack)
{
for(int i = 0; i < 5; i++)
{
stack.push(i);
}
}
if(pos == -1)
System.out.println("Element not found");
else
System.out.println("Element is found at position: " + pos);
}
stack_push(stack);
stack_pop(stack);
stack_push(stack);
stack_peek(stack);
stack_search(stack, 2);
stack_search(stack, 6);
}
}
3. A class called Employee, which models an employee with an ID, name and salary, is
designed as shown in the following class diagram. The method raiseSalary (percent)
increases the salary by the given percentage. Develop the Employee class and suitable
main method for demonstration.
import java.io.*;
public class Employee {
private String name;
private String jobTitle;
private double salary;
public Employee(String name,
String jobTitle, double salary) {
this.name = name;
this.jobTitle = jobTitle;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String
name) {
this.name = name;
}
public String getJobTitle() {
return jobTitle;
}
public void setJobTitle(String
jobTitle) {
this.jobTitle = jobTitle;
import java.io.*;
public class Employee {
private String name;
private String jobTitle;
private double salary;
public Employee(String name,
String jobTitle, double salary) {
this.name = name;
this.jobTitle = jobTitle;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String
name) {
this.name = name;
}
public String getJobTitle() {
return jobTitle;
}
public void setJobTitle(String
jobTitle) {
this.jobTitle = jobTitle;
import java.io.*;
public class Employee {
private String name;
private String jobTitle;
private double salary;
public Employee(String name,
String jobTitle, double salary) {
this.name = name;
this.jobTitle = jobTitle;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String
name) {
this.name = name;
}
public String getJobTitle() {
return jobTitle;
}
public void setJobTitle(String
jobTitle) {
this.jobTitle = jobTitle;
3. A class called Employee, which
models an employee with an ID,
name and salary, is designed as
shown in the following class
diagram. The method raiseSalary
(percent) increases the salary by
the given percentage. Develop the
Employee class and suitable main
method for demonstration.
Save Filename as as:
EmployeeMain.java
Solution:-
import java.util.Scanner;
class Employee
{
private int id;
private String name;
private double salary;
public Employee (int id, String
name, double salary)
{
this.id = id;
this.name = name;
this.salary = salary;
}
public int
getId ()
{
return id;
}
public Strin
3. A class called Employee, which
models an employee with an ID,
name and salary, is designed as
shown in the following class
diagram. The method raiseSalary
(percent) increases the salary by
the given percentage. Develop the
Employee class and suitable main
method for demonstration.
Save Filename as as:
EmployeeMain.java
Solution:-
import java.util.Scanner;
class Employee
{
private int id;
private String name;
private double salary;
public Employee (int id, String
name, double salary)
{
this.id = id;
this.name = name;
this.salary = salary;
}
public int
getId ()
{
return id;
}
public Strin
public class Employee
{
private int id;
private String name;
private double salary;
else
{
System.out.println("Invalid percentage. Salary remains unchanged.");
}
}
// Default constructor
public MyPoint() {
this.x = 0;
this.y = 0;
}
// Overloaded constructor
public MyPoint(int x, int y) {
this.x = x;
this.y = y;
}
5. Develop a JAVA program to create a class named shape. Create three sub classes
namely: circle, triangle and square, each class has two member functions named draw
() and erase (). Demonstrate 15.09.2023 14.09.2023 Annexure-II 3 3 polymorphism
concepts by developing suitable methods, defining member data and main program.
class Shape
{
void draw()
{
System.out.println(“Shape draw()”);
}
void erase()
{
System.out.println (“ Shape erase()”);
}
}
class Circle extends Shape
{
void draw()
{
System.out.println (“Circle draw()”);
}
void erase()
{
System.out.println (“Circle erase()”);
}
}
class Triangle extends Shape
{
void draw()
{
System.out.println(“Triangle erase()”);
}
}
class Square extends Shape
{
void draw()
{
System.out.println (“Square draw()”);
}
void erase()
{
System.out.println (“Square erase()”);
}
}
public class Shapes
{
public static Shape randshape()
{
switch((int)(Math.random()*3))
{
case 0: return new Circle();
case 1: return new Square();
case 2: return new Triangle();
default : System.out.println(“default”);
return new Shape();
}
}
public static void main (String arg[])
{
Shape s[] = new Shape[9];
for(int i = 0;i< s.length; i++)
s[i] = randshape();
for(int i= 0;i < s.length; i++)
s[i].draw();
}
}
6.Develop a JAVA program to create an abstract class Shape with abstract methods
calculateArea() and calculatePerimeter(). Create subclasses Circle and Triangle that
extend the Shape class and implement the respective methods to calculate the area and
perimeter of each shape
mport java.util.*;
int a,b;
a=s.nextInt();
b=s.nextInt();
area_rect=a*b;
System.out.println("Length of rectangle "+a +"breadth of rectangle "+b);
double area_tri;
a=s.nextInt();
b=s.nextInt();
area_tri=(0.5*a*b);
{
double area_circle;
a=s.nextInt();
area_circle=(3.14*a*a);
System.out.println("Radius of circle"+a);
r.printarea();
t.printarea();
Rectangle.java
this.width = width;
this.height = height;
this.width = width;
this.height = height;
Copy
// Main.java
rectangle.printSize();
rectangle.resizeWidth(150);
rectangle.resizeHeight(200);
rectangle.printSize();
8. Develop a JAVA program to create an outer class with a function display. Create
another class inside the outer class named inner with a function called display and call
the two functions in the main class.
Java Program to Demonstrate Nested class
// Class 1
// Helper classes
class Outer {
// Class 2
// Simple nested inner class
class Inner {
// Print statement
System.out.println("In a nested class method");
}
}
}
// Class 2
// Main class
class Main {
// Main driver method
public static void main(String[] args)
{
9. Develop a JAVA program to raise a custom exception (user defined exception) for
DivisionByZero using try, catch, throw and finally
lass Main {
public static void main(String args[]) {
//try block
try{
System.out.println("::Try block::");
int num=67/0;
System.out.println(num);
}
//catch block
catch(ArithmeticException e){
System.out.println("::Catch block::");
System.out.println("ArithmeticException::Number divided by zero");
}
//finally block
finally{
System.out.println("::Finally block::");
}
System.out.println("Rest of the code continues...");
}
}
10. Develop a JAVA program to create a package named mypack and import &
implement it in a suitable class
//create a package
package my pack;
class Circle
{
double r;
void area()
{
System.out.println("Area of the circle = " + (3.14 * r * r));
}
}
class Square
{
double s;
void area()
{
System.out.println("Area of the Square = " + (s * s));
}
}
class Rectangle
{
double l,b;
void area()
{
System.out.println("Area of the circle = " + (l * b));
}
}
//implements the package
import my pack. Circle;
class Eg
{
public static void main(String a[])
{
Circle c = new Circle();
c.area();
}
}
11. Write a program to illustrate creation of threads using runnable class. (start method
start each of the newly created thread. Inside the run method there is sleep() for
suspend the thread for 500 milliseconds).
class IntThread implements Runnable
{
Thread t;
IntThread()
{ t = new Thread ( this, “Test thread”);
System.out.println (“ Child thread :” + t);
t.start();
}
public void run()
{ try
{
for (int i= 5; i0; j--)
{ System.out.println (“Main thread :” + j); Thread.sleep (1000); } } catch
(InterruptedException e) { } System.out.println ( “Main thread exiting …”); } }
12. Develop a program to create a class MyThread in this class a constructor, call the
base class constructor, using super and start the thread. The run method of the class
starts after this. It can be observed that both main thread and created child thread are
executed concurrently.
@Override
@SuppressWarnings("deprecation")
public void run() {
while (running) {
try {
// Suppress deprecation warning for Thread.sleep()
Thread.sleep(500);
System.out.println("Thread ID: " + Thread.currentThread().getId() + " is
running.");
} catch (InterruptedException e) {
System.out.println("Thread interrupted.");
}
}
}