0% found this document useful (0 votes)
8 views18 pages

Task1 - 921321205074

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)
8 views18 pages

Task1 - 921321205074

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/ 18

1)

TASK 2:

package uniquedigit;
import java.util.*;
abstract class Shape {
abstract void rectangleArea(int length, int breadth);
abstract void squareArea(int side);
abstract void circleArea(double radius);
}

class Area extends Shape {


void rectangleArea(int length, int breadth) {
int area = length * breadth;
System.out.println(area);
}

void squareArea(int side) {


int area = side * side;
System.out.println(area);
}

void circleArea(double radius) {


double area = Math.PI * radius * radius;
System.out.println(String.format("%.2f", area));
}
}

public class Main {


public static void main(String[] args) {
Area area = new Area();
Scanner sc = new Scanner(System.in);

int length = sc.nextInt();


int breadth = sc.nextInt();
int side = sc.nextInt();
double radius = sc.nextDouble();

area.rectangleArea(length, breadth);
area.squareArea(side);
area.circleArea(radius);
}
}
Output:
5
4
3
3.0
20
9
28.27

2)
import java.util.*;

interface ShapeCalculator {
void calc(int n);
}

class Square implements ShapeCalculator {


public void calc(int side) {
int area = side * side;
int perimeter = 4 * side;
System.out.println(area + " " + perimeter);
}
}

class Circle implements ShapeCalculator {


public void calc(int radius) {
double area = Math.PI * radius * radius;
double perimeter = 2 * Math.PI * radius;
System.out.println(String.format("%.2f", area) + " " +
String.format("%.2f", perimeter));
}
}

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int input = sc.nextInt();

Square square = new Square();


Circle circle = new Circle();

square.calc(input);
circle.calc(input);
}
}
Output:
5
25 20
78.54 31.42
3)
import java.util.*;

abstract class Complex {


double real1, imag1, real2, imag2;

Complex(double r1, double i1, double r2, double i2) {


this.real1 = r1;
this.imag1 = i1;
this.real2 = r2;
this.imag2 = i2;
}

abstract void add();


abstract void subtract();
abstract void multiply();
abstract void divide();
}

class Addition extends Complex {


Addition(double r1, double i1, double r2, double i2) {
super(r1, i1, r2, i2);
}

void add() {
double real = real1 + real2;
double imag = imag1 + imag2;
System.out.println("Addition:\n\t" +
String.format("%.4f", real) + " +" + String.format("%.4f", imag)
+ " i");
}

void subtract() {}
void multiply() {}
void divide() {}
}

class Subtraction extends Complex {


Subtraction(double r1, double i1, double r2, double i2) {
super(r1, i1, r2, i2);
}
void subtract() {
double real = real1 - real2;
double imag = imag1 - imag2;
System.out.println("Subtraction:\n\t" +
String.format("%.4f", real) + " +" + String.format("%.4f", imag)
+ " i");
}

void add() {}
void multiply() {}
void divide() {}
}

class Multiplication extends Complex {


Multiplication(double r1, double i1, double r2, double i2) {
super(r1, i1, r2, i2);
}

void multiply() {
double real = real1 * real2 - imag1 * imag2;
double imag = real1 * imag2 + imag1 * real2;
System.out.println("Multiplication:\n\t" +
String.format("%.4f", real) + " +" + String.format("%.4f", imag)
+ " i");
}

void add() {}
void subtract() {}
void divide() {}
}

class Division extends Complex {


Division(double r1, double i1, double r2, double i2) {
super(r1, i1, r2, i2);
}

void divide() {
double denominator = real2 * real2 + imag2 * imag2;
double real = (real1 * real2 + imag1 * imag2) /
denominator;
double imag = (imag1 * real2 - real1 * imag2) /
denominator;
System.out.println("Division:\n\t" +
String.format("%.4f", real) + " +" + String.format("%.4f", imag)
+ " i");
}
void add() {}
void subtract() {}
void multiply() {}
}

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

double r1 = sc.nextDouble();
double i1 = sc.nextDouble();
double r2 = sc.nextDouble();
double i2 = sc.nextDouble();

Addition add = new Addition(r1, i1, r2, i2);


add.add();

Subtraction sub = new Subtraction(r1, i1, r2, i2);


sub.subtract();

Multiplication mul = new Multiplication(r1, i1, r2, i2);


mul.multiply();

Division div = new Division(r1, i1, r2, i2);


div.divide();
}
}
Ouput:
3.8
7.0
4.9
6.0
Addition:
8.7000 +13.0000 i
Subtraction:
-1.1000 +1.0000 i
Multiplication:
-23.3800 +57.1000 i
Division:
1.0102 +0.1916 i

4)
import java.util.*;
abstract class Marks {
abstract double getPercentage();
}

class A extends Marks {


private int marks1, marks2, marks3;

A(int m1, int m2, int m3) {


this.marks1 = m1;
this.marks2 = m2;
this.marks3 = m3;
}

double getPercentage() {
return (marks1 + marks2 + marks3) / 3.0;
}
}

class B extends Marks {


private int marks1, marks2, marks3, marks4;

B(int m1, int m2, int m3, int m4) {


this.marks1 = m1;
this.marks2 = m2;
this.marks3 = m3;
this.marks4 = m4;
}

double getPercentage() {
return (marks1 + marks2 + marks3 + marks4) / 4.0;
}
}

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int m1 = sc.nextInt();
int m2 = sc.nextInt();
int m3 = sc.nextInt();
A studentA = new A(m1, m2, m3);
System.out.println(String.format("%.2f",
studentA.getPercentage()));

int m4 = sc.nextInt();
B studentB = new B(m1, m2, m3, m4);
System.out.println(String.format("%.2f",
studentB.getPercentage()));
}
}

Output:
4
5
4
4.33

5)
package uniquedigit;
import java.util.*;

import java.util.Arrays;

interface SortInterface {
int minFrontMoves(int[] arr);
}

class Sorter implements SortInterface {


public int minFrontMoves(int[] arr) {
int n = arr.length;
int[] sortedArr = arr.clone();
Arrays.sort(sortedArr);

int moves = 0;
for (int i = 0; i < n; i++) {
if (arr[i] != sortedArr[moves]) {
moves++;
}
}
return moves;
}
}

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}

Sorter sorter = new Sorter();


System.out.println(sorter.minFrontMoves(arr));
}
}

Output:
5
23 45 34 45 21
4

6)
import java.util.*;

abstract class First {


abstract void input(int a, int b);
abstract void add();
abstract void result();
}

class SubClass extends First {


int num1, num2, sum;

void input(int a, int b) {


num1 = a;
num2 = b;
}

void add() {
sum = num1 + num2;
}

void result() {
System.out.println(sum);
}
}

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();

SubClass sub = new SubClass();


sub.input(a, b);
sub.add();
sub.result();
}
}

Output: 4 5
9
7)
import java.util.Scanner;

//Abstract class ItemType


abstract class ItemType {
public abstract double calculateAmount();
}

//Class Wooden extending ItemType


class Wooden extends ItemType {
private int quantity;
private double costPerUnit;
private double basePrice;

// Constructor
public Wooden(int quantity, double costPerUnit, double
basePrice) {
this.quantity = quantity;
this.costPerUnit = costPerUnit;
this.basePrice = basePrice;
}

// Implement calculateAmount method


@Override
public double calculateAmount() {
return (quantity * costPerUnit) + basePrice;
}
}

//Class Electronics extending ItemType


class Electronics extends ItemType {
private String type;
private double basePrice;
private double additionalCharge;

// Constructor
public Electronics(String type, double basePrice, double
additionalCharge) {
this.type = type;
this.basePrice = basePrice;
this.additionalCharge = additionalCharge;
}

// Implement calculateAmount method


@Override
public double calculateAmount() {
return basePrice + additionalCharge;
}
}

//Driver class Main


public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Get the choice of input


int choice = scanner.nextInt();

// Calculate based on choice


if (choice == 1) {
// Input for Wooden class
int quantity = scanner.nextInt();
double costPerUnit = scanner.nextDouble();
double basePrice = scanner.nextDouble();

Wooden woodenItem = new Wooden(quantity, costPerUnit,


basePrice);
System.out.printf("%.1f", woodenItem.calculateAmount());
} else if (choice == 2) {
// Input for Electronics class
String type = scanner.next();
double basePrice = scanner.nextDouble();
double additionalCharge = scanner.nextDouble();

Electronics electronicsItem = new Electronics(type,


basePrice, additionalCharge);
System.out.printf("%.1f",
electronicsItem.calculateAmount());
}
scanner.close();
}
}

Output:
1
10
1000
10000.0
20000.0

Task1:
1)
import java.util.Scanner;

interface Employee {
void getDetails();
int computeSalary();
}

class PermanentEmployee implements Employee {


private String name;
private int id;
private int basicSalary;
private int bonus;

public PermanentEmployee(String name, int id, int


basicSalary, int bonus) {
this.name = name;
this.id = id;
this.basicSalary = basicSalary;
this.bonus = bonus;
}

@Override
public void getDetails() {
System.out.println("Name: " + name);
System.out.println("ID: " + id);
}

@Override
public int computeSalary() {
return basicSalary + bonus;
}
}

class ContractEmployee implements Employee {


private String name;
private int id;
private int ratePerHour;
private int hoursWorked;

public ContractEmployee(String name, int id, int ratePerHour,


int hoursWorked) {
this.name = name;
this.id = id;
this.ratePerHour = ratePerHour;
this.hoursWorked = hoursWorked;
}

@Override
public void getDetails() {
System.out.println("Name: " + name);
System.out.println("ID: " + id);
}

@Override
public int computeSalary() {
return ratePerHour * hoursWorked;
}
}

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

// Permanent Employee Input


String pName = sc.nextLine();
int pId = sc.nextInt();
int pBasicSalary = sc.nextInt();
int pBonus = sc.nextInt();
sc.nextLine(); // Consume newline

// Contract Employee Input


String cName = sc.nextLine();
int cId = sc.nextInt();
int cRatePerHour = sc.nextInt();
int cHoursWorked = sc.nextInt();
// Create Employee objects
Employee permEmp = new PermanentEmployee(pName, pId,
pBasicSalary, pBonus);
Employee contEmp = new ContractEmployee(cName, cId,
cRatePerHour, cHoursWorked);

// Display Permanent Employee Details


permEmp.getDetails();
System.out.println("Total salary: " +
permEmp.computeSalary());

// Display Contract Employee Details


contEmp.getDetails();
System.out.println("Total salary: " +
contEmp.computeSalary());

sc.close();
}
}

Output:
keerthana
21
4
10
keerthanadevi
20
5
9
Name: keerthana
ID: 21
Total salary: 14
Name: keerthanadevi
ID: 20
Total salary: 45

2)

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

abstract class Account {


String name;
int number;
int balance;
Date startDate;

Account(String name, int number, int balance, Date startDate)


{
this.name = name;
this.number = number;
this.balance = balance;
this.startDate = startDate;
}

abstract double calculateInterest(Date endDate);


}

class SavingsAccount extends Account {


private final double interestRate = 0.12;

SavingsAccount(String name, int number, int balance, Date


startDate) {
super(name, number, balance, startDate);
}

@Override
double calculateInterest(Date endDate) {
long months = (endDate.getTime() - startDate.getTime()) /
(1000L * 60 * 60 * 24 * 30);
return balance * interestRate * months / 12;
}
}

class CurrentAccount extends Account {


private final double interestRate = 0.05;

CurrentAccount(String name, int number, int balance, Date


startDate) {
super(name, number, balance, startDate);
}

@Override
double calculateInterest(Date endDate) {
long months = (endDate.getTime() - startDate.getTime()) /
(1000L * 60 * 60 * 24 * 30);
return balance * interestRate * months / 12;
}
}

public class Main {


public static void main(String[] args) throws ParseException
{
Scanner sc = new Scanner(System.in);
SimpleDateFormat dateFormat = new
SimpleDateFormat("dd/MM/yyyy");

int accountType = sc.nextInt(); // 1 for Savings, 2 for


Current
sc.nextLine(); // Consume newline
String name = sc.nextLine();
int accountNumber = sc.nextInt();
int balance = sc.nextInt();
sc.nextLine(); // Consume newline
String startDateStr = sc.nextLine();
String endDateStr = sc.nextLine();

Date startDate = dateFormat.parse(startDateStr);


Date endDate = dateFormat.parse(endDateStr);

Account account;
if (accountType == 1) {
account = new SavingsAccount(name, accountNumber,
balance, startDate);
} else {
account = new CurrentAccount(name, accountNumber,
balance, startDate);
}

System.out.println(account.calculateInterest(endDate));
sc.close();
}
}

Output:
1
Karthick
101521502

7000
22/04/2013
22/04/2016
2520.0

3)

import java.util.*;
class Vehicle {
void move(String message) {
System.out.println(message);
}
}

class Motorbike extends Vehicle {


@Override
void move(String message) {
System.out.println(message);
}
}

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input1 = sc.nextLine();
String input2 = sc.nextLine();

Vehicle v = new Vehicle();


v.move(input2); // Print from Vehicle class

Vehicle m = new Motorbike();


m.move(input1); // Print from Motorbike class
(Polymorphism)

sc.close();
}
}

Output:
are sweet
mangoo
mangoo
are sweet
4)
public class Main {
public static void main(String[] args) {
System.out.println("Hi");
main("Hello World");
main("Tom", "Jerry");
}

public static void main(String arg1) {


System.out.println("Overloaded: " + arg1);
}

public static void main(String arg1, String arg2) {


System.out.println("Overloaded: " + arg1 + " & " + arg2);
}
}

Output:
Hi
Overloaded: Hello World
Overloaded: Tom & Jerry

5)
import java.util.*;
class Hello {
void sayHello() {
System.out.println("Hello");
}

void sayHello(String name) {


System.out.println("Hello " + name);
}
}

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();

Hello obj = new Hello();


obj.sayHello(); // First method call
obj.sayHello(input); // Second method call with
input

sc.close();
}
}

Output:
keerthana
Hello
Hello keerthana

You might also like