Unit-II - Practical Pgms
Unit-II - Practical Pgms
Output:
Enter Employee's Name, Employee No, Basic
salary : Madhavan
TS/10
1
32000
Name: Madhavan
Employee Number : TS/101
Gross salary : Rs. 46400.0
Net Salary : Rs. 42560.0
2b. CREATING A PROGRAM TO INITIALIZE THE CONSTRUCTOR
The 'Cabservice' is an organisation that provides 'Online Booking' for the passengers to avail
pick- up and drop facility. Define a class Cabservice having the following specifications:
Up to 1 km 25
More than I km and up to 5 km 30
More than 5 km and up to 10 km 35
More than 10 km and up to 20 km 40
More than 20 km 45
Write the main method to create an object of a class and call all the above member
methods.
Output:
Enter taxi
number TN
2346
Enter name of the
passenger: Anant
Enter distance
travelled: 22
Output:
D:\jdk1.8.0_111\bin>javac list2.java
D:\jdk1.8.0_111\bin>java list2
NAME:M.SENTHILKUMAR
REGNO:1001 MARK1:98 MARK2:99 MARK3:100
PRACTICAL MARK:60
TOTAL :357
THE STUDENT IS PASS
Output:
Area of square with side 4: 16
Area of rectangle with length 5 and width 3: 15
Area of circle with radius 2.5: 19.634954084936208
Area of triangle with base 6.0 and height 4.0: 12.0
Program creates a superclass called Figure that stores the dimensions of various two-
dimensional objects. It also defines a method called area() that computes the area of an object.
The program derives two subclasses from Figure. The first is Rectangle and the second is
Triangle. Each of these subclasses overrides area() so that it returns the area of a rectangle and
triangle respectively.
class Figure
{
double dim1;
double dim2;
Figure(double a,double b)
{
dim1=a;
dim2=b;
}
double area()
{
System.out.println("Area for Figure is undefined");
return 0;
}
}
double area()
{
System.out.println("Inside Area for Triangle");
return dim1 * dim2/2;
}
}
class Findareas {
public static void main(String[] args) {
Figure f=new Figure(10,10);
Rectangle r= new Rectangle(9,5);
Triangle t=new Triangle(10,8);
Figure figref;
figref=r;
System.out.println("Area is"+figref.area());
figref=t;
System.out.println("Area is"+figref.area());
figref=f;
System.out.println("Area is"+figref.area());
}
}
Output:
Inside Area for Rectangle
Area is 45.0
Inside Area for Triangle
Area is 40.0
Area for Figure is undefined
Area is 0.0
Write a Java program that demonstrates abstraction through a calculator with user input. The
program includes an abstract class Calculator, concrete classes for addition, subtraction,
multiplication, and division, and a main class to perform these operations based on user input.
import java.util.Scanner;
// Constructor
public Calculator(double number1, double number2) {
this.number1 = number1;
this.number2 = number2;
}
// Addition class
class Addition extends Calculator {
public Addition(double number1, double number2) {
super(number1, number2);
}
@Override
double calculate() {
return number1 + number2;
}
}
// Subtraction class
class Subtraction extends Calculator {
public Subtraction(double number1, double number2) {
super(number1, number2);
}
@Override
double calculate() {
return number1 - number2;
}
}
// Multiplication class
class Multiplication extends Calculator {
public Multiplication(double number1, double number2) {
super(number1, number2);
}
@Override
double calculate() {
return number1 * number2;
}
}
// Division class
class Division extends Calculator {
public Division(double number1, double number2) {
super(number1, number2);
}
@Override
double calculate() {
if (number2 != 0) {
return number1 / number2;
} else {
System.out.println("Error: Division by zero");
return Double.NaN;
}
}
}
// Main class
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Calculator calculator;
scanner.close();
}
}
Output:
Enter first number: 10
Enter second number: 5
Enter operation (+, -, *, /): +
Result: 15.0
2g. A Java program for managing airline ticketing details using inheritance. This program
involves a base class Ticket, subclasses DomesticTicket and InternationalTicket, and a main
class to handle user input and display the details.
import java.util.Scanner;
// Base class Ticket
class Ticket
{
String passengerName;
String flightNumber;
double basePrice;
// Constructor
public Ticket(String passengerName, String flightNumber, double basePrice)
{
this.passengerName = passengerName;
this.flightNumber = flightNumber;
this.basePrice = basePrice;
}
// Constructor
public DomesticTicket(String passengerName, String flightNumber, double basePrice,
double taxRate)
{
super(passengerName, flightNumber, basePrice);
this.taxRate = taxRate;
}
// Subclass InternationalTicket
class InternationalTicket extends Ticket
{
double surcharge; // Surcharge for international flights
// Constructor
public InternationalTicket(String passengerName, String flightNumber, double basePrice,
double surcharge)
{
super(passengerName, flightNumber, basePrice);
this.surcharge = surcharge;
}
// Main class
public class Main
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
// Displaying details
System.out.println("\nDomestic Ticket Details:");
domesticTicket.displayDetails();
scanner.close();
}
}
Output:
Enter details for Domestic Ticket:
Passenger Name: David
Flight Number: SwissA234
Base Price: 3456
Tax Rate (in %): 23