Vous êtes sur la page 1sur 9

1)

StudentDemo.java
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);
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();
}
}

Student.java
package com;
public class Student {
private int rollNo;
private String name;
private double marks;

public int getRollNo() {


return rollNo;
}
public String getName() {
return name;
}

public double getMarks() {


return marks;
public void setMarks(double marks) {
this.marks = marks;
}
public Student(int rollNo,String name,double marks) {
// TODO Auto-generated constructor stub
this.rollNo = rollNo;
this.name = name;
this.marks = marks;
}

}
2)
Toy,java
package com;
public class Toy {
private String name,category;
private double price,discount;
public String getName() {
return name;
}

public String getCategory() {


return category;
}

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 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;
}

}
ToyDemo.java
package com;
public class ToyDemo {

public static void main(String[] args) {


// TODO Auto-generated method stub
Toy one=new Toy("Banana","fruit",100,10.5);
Toy two=new Toy("Orange","fruit",300,30);
Toy three=new Toy("Lion","animal",50,5);
Toy four=new Toy("Tiger","animal",70,7);
System.out.println(getLeastPriceToy(one, two, three, four,"fruit"));
System.out.println(getLeastPriceToy(one, two, three, four,"animal"));
}
public static String getLeastPriceToy(Toy one, Toy two, Toy three, Toy four,
String category) {
// TODO Auto-generated method stub
Toy ansToy = one;
if(two.getCategory().equals(category) &&
(two.getPrice()*two.getDiscount())<(ansToy.getPrice()*ansToy.getDiscount())){
ansToy =two;
}
if(three.getCategory().equals(category) &&
(three.getPrice()*three.getDiscount())<(ansToy.getPrice()*ansToy.getDiscount())){
ansToy =three;
}
if(four.getCategory().equals(category) &&
(four.getPrice()*four.getDiscount())<(ansToy.getPrice()*ansToy.getDiscount())){
ansToy =four;
}
return ansToy.getName();
}
}

3)
Car.java
package com;
public class Car {
private String make,model;

private int passengerCapacity;


private 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 setOnRoadPice(double onRoadPrice) {
this.onRoadPrice = onRoadPrice;
}
public String getMake() {
return make;
}
public String getModel() {
return model;
}

}
CarDemo.javapackage com;

public class CarDemo {


public CarDemo() {
// TODO Auto-generated constructor stub
}

public static void main(String[] args) {


// TODO Auto-generated method stub
Car one =new Car("Hyundai","Santro",5,5.5);
Car two =new Car("Hyundai","accent",6,7.5);
Car three =new Car("Maruti","Wagnonr",6,5.5);
Car four =new Car("Maruti","echo",7,4.5);
char compareType = 'c';
System.out.println(bestCar(one,two,three,four,compareType));
}
private static String bestCar(Car one, Car two, Car three, Car four,
char compareType) {
// TODO Auto-generated method stub
Car temp=one;
if(compareType=='c'){
if(two.getPassengerCapacity()>temp.getPassengerCapacity()){
temp=two;
}
if(three.getPassengerCapacity()>temp.getPassengerCapacity()){
temp=three;
}
if(four.getPassengerCapacity()>temp.getPassengerCapacity()){
temp=four;
}
return temp.getMake()+'-'+temp.getModel();
}
else if(compareType=='p'){
if(two.getOnRoadPrice()>temp.getOnRoadPrice()){
temp=two;
}
if(three.getOnRoadPrice()>temp.getOnRoadPrice()){
temp=three;
}
if(four.getOnRoadPrice()>temp.getOnRoadPrice()){
temp=four;
}

return temp.getMake()+'-'+temp.getModel();
}
else{
return "Wrong Compare Type";
}
}
}
4)
Customer.java
package com;
public class Customer {
private int custId,accId;
private double creditCardCharges;
public Customer(int accId,int custId,double creditCardCharges) {
// TODO Auto-generated constructor stub
this.accId=accId;
this.custId=custId;
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;
}
}

CreditCardDemo.java
package com;
import com.CreditCardCompany;
public class CreditCardDemo {

public static void main(String[] args) {


// TODO Auto-generated method stub
Customer c1 =new Customer(123,456,2600);
Customer c2 =new Customer(789,324,2000);
Customer c3 =new Customer(290,178,700);
Customer c4 =new Customer(330,190,1700);
Customer c5 =new Customer(330,190,1250);
System.out.println("customer 1 charge 2600 payback is
"+CreditCardCompany.getPaybackAmount(c1));
System.out.println("customer 2 charge 2000 payback is
"+CreditCardCompany.getPaybackAmount(c2));
System.out.println("customer 3 charge 700 payback is
"+CreditCardCompany.getPaybackAmount(c3));
System.out.println("customer 4 charge 1700 payback is
"+CreditCardCompany.getPaybackAmount(c4));
System.out.println("customer 5 charge 1250 payback is
"+CreditCardCompany.getPaybackAmount(c5));
}
}

CreditCardCompany.java
package com;
public class CreditCardCompany {
public static double getPaybackAmount(Customer c){
double payback,remainingCharge,tempPercent;
int listRange[]={500,1000,1000,0},i;
remainingCharge=c.getCreditCardCharges();
tempPercent=0.25;
i=0;
payback=0;

remainingCharge-=listRange[i];
//if remaining charge is greater than limit given it means whole slot must
be added in payback
while(remainingCharge>0 && i<3){

//

//System.out.println(remainingCharge);
payback=payback+(listRange[i]*tempPercent/100);
System.out.println(payback);
tempPercent=tempPercent+0.25;
i++;
remainingCharge-=listRange[i];

}
payback+=((remainingCharge+listRange[i])*tempPercent/100);
return payback;
}
}

Vous aimerez peut-être aussi