0% found this document useful (0 votes)
86 views

Assn 1

The document contains code for four Java classes - CarDemo, CreditCardDemo, StudentDemo, and ToyDemo. Each class contains a main method that creates objects of other classes, calls methods on those objects to compare or calculate values, and prints the results. The classes Car, Customer, Student, and Toy are also defined, with fields and getter/setter methods to represent different object types for the demo classes to work with.

Uploaded by

vamsirathan
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)
86 views

Assn 1

The document contains code for four Java classes - CarDemo, CreditCardDemo, StudentDemo, and ToyDemo. Each class contains a main method that creates objects of other classes, calls methods on those objects to compare or calculate values, and prints the results. The classes Car, Customer, Student, and Toy are also defined, with fields and getter/setter methods to represent different object types for the demo classes to work with.

Uploaded by

vamsirathan
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/ 9

package com;

import java.util.Scanner;
public class CarDemo
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
Car one =new Car("maruti","wagonr",9,9.50);
Car two =new Car("chevrolet","beat",10,10.50);
Car three =new Car("tata","nano",10,5.50);
Car four =new Car("hyundai","santro",5,5.40);
CarDemo cd=new CarDemo();//can call non-static method in
main by using object of the same class(where main is defined)
System.out.println("For highest value "+cd.BestCar(one, two,
three, four,'p'));
}
public String BestCar(Car one,Car two,Car three,Car four,char
compareType)
{
Scanner sc= new Scanner(System.in); //if kept in main then
bestcar() is taking argument p only
System.out.println("Enter Type:");
compareType=sc.next().charAt(0);
sc.close();
if (compareType=='c')
{
Car st=one;
if
(two.getPassengerCapacity()>=st.getPassengerCapacity())
st=two;
if
(three.getPassengerCapacity()>=st.getPassengerCapacity())
st=three;
if
(four.getPassengerCapacity()>=st.getPassengerCapacity())
st=four;
//int max=st.getPassengerCapacity();
//System.out.println(max);
//String x=st.getMake();
//if (max==two.getPassengerCapacity() & !
x.equals(two.getMake()))

two.getModel();

//return "of c:"+two.getMake() +"-"+

return "of c:"+st.getMake() +"-"+ st.getModel();


//returning multiple return values
}
else if(compareType=='p')
{
Car st=one;
if (two.getOnRoadPrice()>st.getOnRoadPrice())
st=two;
if (three.getOnRoadPrice()>st.getOnRoadPrice())
st=three;
if (four.getOnRoadPrice()>st.getOnRoadPrice())
st=four;
return "of p:"+st.getMake() +"-"+ st.getModel();
}
else

return "wrong choice";

}
}
class Car{
private
private
private
private

String make;
String model;
int passengerCapacity;
double onRoadPrice;

public Car(String make,String model,int passengerCapacity,double


onRoadPrice)
{
// TODO Auto-generated constructor stub
this.make=make;
this.model=model;
this.passengerCapacity=passengerCapacity;
this.onRoadPrice=onRoadPrice;
}
public int getPassengerCapacity()
{
return passengerCapacity;
}
public void setPassengerCapacity(int passengerCapacity)
{

this.passengerCapacity=passengerCapacity;

public double getOnRoadPrice()


{
return onRoadPrice;
}
public void setOnRoadPrice(double onRoadPrice )
{
this.onRoadPrice=onRoadPrice;
}
public String getMake()
{
return make;
}
public String getModel()
{
return model;
}
}
-------------------------------------------package com;
public class CreditCardDemo
{
public static void main(String[] args)
{
CreditCardCompany ccc=new CreditCardCompany();
// TODO Auto-generated method stub
Customer c1 =new Customer(1,987,400.0);
Customer c2 =new Customer(2,654,1400.0);
Customer c3 =new Customer(3,321,2000.0);
Customer c4 =new Customer(4,624,2600.0);
Customer c5 =new Customer(5,759,3000.0);
//for(int i=1;i<6;i++)
//http://stackoverflow.com/questions/19105401/how-would-i-create-anew-object-from-a-class-using-a-for-loop-in-java
System.out.println("Payback amount is:
"+ccc.getPaybackAmount(c1));
System.out.println("Payback amount is:
"+ccc.getPaybackAmount(c2));
System.out.println("Payback amount is:
"+ccc.getPaybackAmount(c3));

System.out.println("Payback amount is:


"+ccc.getPaybackAmount(c4));
System.out.println("Payback amount is:
"+ccc.getPaybackAmount(c5));
}
}
class Customer{
private int custId;
private int accId;
private double creditCardCharges;
public Customer(int custId,int accId,double creditCardCharges)
{
// TODO Auto-generated constructor stub
this.custId =custId;
this.accId = accId;
this.creditCardCharges =creditCardCharges;
}
public double getCreditCardCharges()
{
return creditCardCharges;
}
public void setCreditCardCharges(double creditCardCharges)
{
this.creditCardCharges=creditCardCharges;
}
public int getCustId()
{
return custId;
}

public int getAccId()


{
return accId;
}

class CreditCardCompany
{
public double getPaybackAmount(Customer cust)
{
double x;
x=cust.getCreditCardCharges();

if (x<=500)
return x*0.25*0.01;
else if(x>500 & x<=1500)
return 1.25+(x-500)*0.50*0.01;
else if(x>1500 & x<=2500)
return 1.25+5.00+(x-1500)*0.75*0.01;
else

return 1.25+5.00+7.50+(x-2500)*1.00*0.01;

}
}
-----------------------------------------package com;
public class StudentDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Student one = new Student(1, "ravi", 45);
Student two = new Student(2, "amit", 65);
Student three = new Student(3, "pooja", 55);
//trainees may use scanner object to read above values.
//However, use separate scanner for String and numbers
System.out.println("Student with highest marks is " +
compareStudents(one, two, three));
}
public static String compareStudents(Student one, Student two,
Student three)
{
Student st = one;
if(two.getMarks() >st.getMarks())
st = two;
if(three.getMarks() > st.getMarks())
st = three;
return st.getName();
}
}
class Student
{
private int rollNo;
private String name;
private double marks;
public Student(int rollNo, String name, double marks)

{
this.rollNo = rollNo;
this.name = name;
this.marks = marks;
}
public double getMarks()
{
return marks;
}
public void setMarks(double marks)
{
this.marks = marks;
}
public int getRollNo()
{
return rollNo;
}
public String getName()
{
return name;
}
}
----------------------------------------------package com;
import java.util.Scanner;
public class ToyDemo
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
Toy one=new Toy("Apple","fruits",20.0,0.02);
Toy two=new Toy("Banana","fruits",150.0,0.03);
Toy three= new Toy("monkey","animals",200.0,0.50);
Toy four=new Toy("lion","animals",15.0,0.50);
System.out.println("Least Price Toy name is " +
getLeastPriceToy(one, two, three, four,""));
}
public static String getLeastPriceToy(Toy one,Toy two,Toy
three,Toy four,String category_name)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter category name:");

category_name=sc.next(); //If you put a String before


the variable name it is a declaration.
//And you can declare a variable name only once in a
scope.
sc.close();
//Toy st=one; //cannot covert toy to string(kept
String st)
if (category_name.equals(one.getCategory()) ||
category_name.equals(two.getCategory()))
{
System.out.println("Fruits category found ");
if (two.getPrice()*(1two.getDiscount())<one.getPrice()*(1-one.getDiscount()))
return two.getName();
else
return one.getName();
}
else if(category_name.equals(three.getCategory()) ||
category_name.equals(four.getCategory()))
{
System.out.println("Animals category found
");
if (three.getPrice()*(1three.getDiscount())<four.getPrice()*(1-four.getDiscount()))
return three.getName();
else
return four.getName();
}
else
{
System.out.println("no category found ");
return "no category";
}
/*if (two.getPrice()*(1two.getDiscount())<st.getPrice()*(1-st.getDiscount()))
st=two;
if (three.getPrice()*(1three.getDiscount())<st.getPrice()*(1-st.getDiscount()))
st=three;
if (two.getPrice()*(1two.getDiscount())<st.getPrice()*(1-st.getDiscount()))
st=four;
return st.getName();*/

}
}
class Toy
{
private
private
private
private

String
String
double
double

name;
category;
price;
discount;

public Toy(String name,String category,double price,double


discount)
{
// TODO Auto-generated constructor stub
this.name= name;
this.category= category;
this.price= price;
this.discount= discount;
}
public double getPrice()
{
return price;
}
public void setPrice(double price)
{
this.price=price;
}
public double getDiscount()
{
return discount;
}
public void setDiscount(double discount)
{
this.discount=discount;
}
public String getName()
{
return name;
}
public String getCategory()
{
return category;
}

You might also like