Hands On Inheritance, Polymorphism, Abstract Class, Interface
Hands On Inheritance, Polymorphism, Abstract Class, Interface
public class Doctor extends Person // inherit name from Person in Doctor class
{
String specializationType;
//inlcude the required attribute and method as per the problem statement
public String displayDetails(){
return name + "is a " + specializationType;
}
}
-------------------------------------------------------------
Multiplier - Overloading
//include the overloaded methods and perform the logic as specified in the
problem statement
public static int multiply(int x,int y){
return(x*y);
}
public static int multiply(int x,int y,int z){
return(x*y*z);
}
public static double multiply(double x,double y){
return(x*y);
}
}
----------------------------------------------------------------
PersonIsADoctor-Abstract
public class Doctor extends Person// inherit name from Person in Doctor class
{
//inlcude the required attribute and method as per the problem statement
String specializationType;
public Doctor(String name,String specializationType){
super(name);
this.specializationType=specializationType;
}
public String getSpecializationType(){
return specializationType;
}
public String displayDetails(){
return super.getName()+" is a "+getSpecializationType();
}
}
-----------------------------------------------------------------------------------
-----------------------
Printer-Interface
//create a class ,override the print() method and return the String as expected in
the problem statement
class LaserPrinter implements Printer{
}
-----------------------------------------------------------------------------------
------------------------
import java.util.Scanner;
public class Main{
public static Hosteller getHostellerDetails()
{
Hosteller h=new Hosteller();
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Details:");
System.out.println("Student Id");
int a=sc.nextInt();
System.out.println("Student Name");
sc.nextLine();
String b=sc.nextLine();
System.out.println("Department Id");
int c=sc.nextInt();
System.out.println("Gender");
sc.nextLine();
String d=sc.nextLine();
System.out.println("Phone Number");
String e=sc.nextLine();
System.out.println("Hostel Name");
String f=sc.nextLine();
System.out.println("Room Number");
int g=sc.nextInt();
h.setStudentId(a);
h.setName(b);
h.setDepartmentId(c);
h.setGender(d);
h.setPhone(e);
h.setHostelName(f);
h.setRoomNumber(g);
System.out.println("Modify Room Number(Y/N)");
char i=sc.nextLine().charAt(0);
if(i=='Y'){
System.out.println("New Room Number");
int x=sc.nextInt();
h.setRoomNumber(x);
}
System.out.println("Modify Phone Number(Y/N)");
char j=sc.nextLine().charAt(0);
if(j=='Y'){
System.out.println("New Phone Number");
sc.nextLine();
String y=sc.nextLine();
h.setPhone(y);
}
return h;
}
public static void main(String args[])
{
Hosteller h1=new Hosteller();
h1=getHostellerDetails();
System.out.println("The Student Details");
System.out.println(h1.getStudentId()+" "+h1.getName()+"
"+h1.getDepartmentId()+" "+h1.getGender()+" "+h1.getPhone()+" "+h1.getHostelName()
+" "+h1.getRoomNumber());
}
-----------------------------------------------------------------------------------
----------------------------------------------------------------
PassengerDetails
public Passenger (int ticketid, String name, String gender, String address){
this.ticketid=ticketid;
this.name=name;
this.gender=gender;
this.address=address;
}
@Override
public String toString(){
return
"ticketid:"+ticketid+",name:"+name+",gender:"+gender+",address:"+address;
}
public int getTicketid(){
return ticketid;
}
public void setTicketid(int ticketid){
this.ticketid=ticketid;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public String getGender(){
return gender;
}
public void setGender(String gender){
this.gender=gender;
}
public String getAddress(){
return address;
}
public void setAddress(String address){
this.address=address;
}
}
import java.util.*;
import java.util.Scanner;
import java.util.ArrayList;
public class Main{
}
}
-----------------------------------------------------------------------------------
--------
PF and Salary Calculation
System.out.println("NetSalary:"+String.format("%.2f",p.getNetsalary()));
}
}
}
-----------------------------------------------------------------------------------
------------------
import java.util.Scanner;
public class Main{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the name:");
sc.nextLine();
String a =sc.nextLine();
System.out.println("Enter the panno:");
sc.next();
String b =sc.nextLine();
System.out.println("Enter the emailid:");
sc.next();
String c =sc.nextLine();
System.out.println("Enter the salary:");
int d =sc.nextInt();
System.out.println("Enter the name:");
sc.nextLine();
String e =sc.nextLine();
System.out.println("Enter the panno:");
sc.next();
String f =sc.nextLine();
System.out.println("Enter the emailid:");
sc.next();
String g =sc.nextLine();
System.out.println("Enter the salary:");
int h =sc.nextInt();
Customer a1=new Customer(a,b,c,d);
Customer a2=new Customer(e,f,g,h);
boolean a3= true;
a3=a1.equals(a2);
if(a3==true)
System.out.println("Both the objects are equal.");
else
System.out.println("Both the objects are not equal.");
}
}
-----------------------------------------------------------------------------------
----------------------------
import java.lang.Math;
public class Circle extends Shape{
private float radius;
public Circle(float radius){
this.radius=radius;
}
@Override
public double calculateArea(){
return Math.PI*(radius*radius);
}
}
import java.util.Scanner;
-----------------------------------------------------------------------------------
---------------------
//Attributes
private int customerId;
private String customerName;
private String emailId;
//Constructor
public Customer(int customerId, String customerName, String emailId) {
super();
this.customerId = customerId;
this.customerName = customerName;
this.emailId = emailId;
}
}
public class SavingsAccount extends Account{
private double minimumBalance;
boolean output=savacc.withdraw(20000);
System.out.println(output);
}
}
-----------------------------------------------------------------------------------
-----------------------------------------
this.vehicleNumber = vehicleNumber;
this.modelName = modelName;
this.vehicleType=vehicleType;
this.price = price;
}
@Override
public double issueLoan(){
if (vehicleType.startsWith("4")){
return price*80.0/100.0;
}
else if (vehicleType.startsWith("3")){
return price*75.0/100.0;
}
else if (vehicleType.startsWith("2")){
return price*50.0/100.0;
}
return 0;
}
@Override
public double takeInsurance(){
if(price<=150000){
return 3500;
}
else if(price>150000 && price<=300000){
return 4000;
}
else if(price>300000){
return 5000;
}
return 0;
}
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Vehicle Vehi = new Vehicle("AS1234","Hero","2 wheeler",300000);
System.out.println(Vehi.issueLoan());
System.out.println(Vehi.takeInsurance());
}
}
-----------------------------------------------------------------------------------
--------------------------------------
//}
public double calculateLoanAmount(Employee employeeObj){
double loan =0.0;
if(employeeObj instanceof PermanentEmployee){
loan=employeeObj.getSalary()*15.0/100.0;
}
if(employeeObj instanceof TemporaryEmployee){
loan=employeeObj.getSalary()*10.0/100.0;
}
return loan;
}
import java.util.Scanner;
public class Main{
System.out.println(peremloan);
System.out.println(tememloan);
-----------------------------------------------------------------------------------
-----------------------------------
import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.ParseException;
import java.util.Scanner;
sf.setLenient(false);
//String date= "31/08/2020";
String date=sc.next();
try{
sf.parse(date);
System.out.println(date+" is a valid date");
}
catch(ParseException e){
System.out.println(date+" is not a valid date");
}
//FILL THE CODE
-----------------------------------------------------------------------------------
-------------------
import java.util.*;
import java.text.*;
}
catch(ParseException e){
System.out.println(e);
}
//FILL THE CODE
-----------------------------------------------------------------------------------
-----------------------------
import java.util.Scanner;
import java.text.*;
import java.util.*;
}
------------------------------------------------------
*--------------------------------------------------------