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

Java Cycle 2

The document contains multiple Java code snippets demonstrating various programming concepts such as area calculation for different shapes, employee management with inheritance, method overriding, abstract classes, interfaces, and garbage collection. Each code snippet includes a main method that executes specific functionalities related to the defined classes. The examples illustrate the use of object-oriented programming principles in Java.

Uploaded by

WITCHER
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Java Cycle 2

The document contains multiple Java code snippets demonstrating various programming concepts such as area calculation for different shapes, employee management with inheritance, method overriding, abstract classes, interfaces, and garbage collection. Each code snippet includes a main method that executes specific functionalities related to the defined classes. The examples illustrate the use of object-oriented programming principles in Java.

Uploaded by

WITCHER
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

CODE:

import java.util.*;
class Area_Calculator{

public void Calcluate_Area(double radius){


System.out.println(3.14*radius*radius);
}

public void Calcluate_Area(double length, double width){

System.out.println(length*width);
}

public void Calcluate_Area(double a,double b, double c){


double s = (a+b+c)/2;

System.out.println(Math.sqrt(s*(s-a)*(s-b)*(s-c)));

}
}

class shape{
public static void main(String[] args){
Area_Calculator calculator = new Area_Calculator();
//calculator.Calcluate_Area(5,4,3);

Scanner my_scanner = new Scanner(System.in);

System.out.println("Choose and option\n " );


System.out.println("1- Calculate area of circle " );
System.out.println("2- Caclculate area of rectangle " );
System.out.println("3-calculate area of traingle " );
int num = my_scanner.nextInt();
switch(num) {
case 1:

System.out.println("Enter the radius of circle: " );


double r=my_scanner.nextDouble();
calculator.Calcluate_Area(r);
break;
case 2:
double length,breadth;
System.out.println("Enter the length and breadth of rectanglie: " );
length=my_scanner.nextDouble();
breadth=my_scanner.nextDouble();
System.out.print("Area: " );
calculator.Calcluate_Area(length,breadth);
break;

case 3:
System.out.println("Enter the length of three sides of triangle: " );
double a = my_scanner.nextDouble();
double b = my_scanner.nextDouble();
double c = my_scanner.nextDouble();
System.out.println("Area: ");
calculator.Calcluate_Area(a,b,c);
break;

}
}

OUTPUT:
CODE:
import java.util.*;
class Employee{
String name;
int age;
String address;
int salary;
String phone_number;

Employee(String name, int age, String address, int salary,String phone_number){


this.name = name;
this.age = age;
this.address = address;
this.salary = salary;
this.phone_number = phone_number;
}

public void print_salary(){


System.out.println("Salary: " + salary);
}

public void print_details(){


System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("address: " + address);
System.out.println("Phone Number: " + phone_number);
}
}
class Officer extends Employee{
String specialization;
Officer(String specialization,String name, int age, String address, int salary,
String phone_number){
super(name, age, address, salary, phone_number);
this.specialization = specialization;
}
public void print_details(){
super.print_details();
System.out.println("Specialization: " + specialization);
}
}

class Manager extends Employee{


String department;
Manager(String name, int age, String address , int salary,String
phone_number,String department){
super(name,age,address,salary,phone_number);
this.department = department;
}
public void print_details(){
super.print_details();
System.out.println("Deoartment: " + department);
}
}

class pr2{
public static void main(String[] args){
Officer officer1 = new Officer("Developer","Adil",23,"Trivandrum kerela
India",210000,"9074130161");
officer1.print_details();

System.out.println();
Manager manager1 = new Manager("Aflah",18,"Kozhikode kerela
India",1000000,"8547184423","Sales");
manager1.print_details();
}
}
OUTPUT:
CODE:
import java.util.*;
class Employee{
String salary;

void display(){
System.out.println("The class is Employee");
}
void calcSalary(){

System.out.println("Salary of the employee is 10,000");


}

class Engineer extends Employee{

void calcSalary(){

System.out.println("The salary of the employee is 20,000");


}
}

class pr3{
public static void main(String[] args){
Engineer engineer1 = new Engineer();

engineer1.calcSalary();
engineer1.display();
}
}
OUTPUT:
CODE:
import java.util.*;

abstract class Shape{


abstract void numberOfSides();

}
class rectangle extends Shape{
void numberOfSides(){
System.out.println("the number of sides of rectangle is 4");
}
}
class triangle extends Shape{
void numberOfSides(){
System.out.println("The number of sides of triangle is 3");
}
}
class hexagon extends Shape{
void numberOfSides(){
System.out.println("THe number of sides of hexagon is 6");
}
}
class pr4{
public static void main(String[] args){
rectangle my_rectangle = new rectangle();
triangle my_triangle = new triangle();
hexagon my_hexagon = new hexagon();
Scanner sc = new Scanner(System.in);
int num;
while(true){
System.out.println("1- Rectangle: ");
System.out.println("2- Triangle: ");
System.out.println("3- Hexagon: ");
System.out.println("4- EXIT: ");
num = sc.nextInt();
switch(num){
case 1:
my_rectangle.numberOfSides();
System.out.print("---------------------------\n");
break;
case 2:
my_triangle.numberOfSides();
System.out.print("---------------------------\n");
break;
case 3:
my_hexagon.numberOfSides();
System.out.print("---------------------------\n");
break;
case 4:
return;
}//switch
}//while
}
}

OUTPUT:
CODE:
interface Employee {
void work();
}
interface TeachingStaff extends Employee {
void teach();
}
class Professor implements TeachingStaff {
public void work() {
System.out.println("Professor is working");
}
public void teach() {
System.out.println("Professor is teaching");
}
}
public class pr5 {
public static void main(String[] args) {
Professor professor = new Professor();
professor.work();
professor.teach();
}
}

OUTPUT:
CODE:
class Demo {
protected void finalize() throws Throwable {
System.out.println("Garbage collected: " + this);
}
}

class exp6 {
public static void main(String[] args) {
Demo obj1 = new Demo();
Demo obj2 = new Demo();

obj1 = null;
obj2 = null;

System.gc();
System.out.println("End of main method.");
}
}

OUTPUT:

You might also like