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

assn1

Embed Size (px)

DESCRIPTION

basic classes and objects

Citation preview

Page 1: assn1

package com;

import java.util.Scanner;

public class CarDemo {

public static void main(String[] args) {

// TODO Auto-generated method stubCar 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 onlySystem.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()))

Page 2: assn1

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

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();

}

elsereturn "wrong choice";

}

}

class Car{private String make;private String 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){

Page 3: assn1

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 stubCustomer 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-a-new-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));

Page 4: assn1

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 stubthis.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();

Page 5: assn1

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 stubStudent 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 numbersSystem.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)

Page 6: assn1

{ 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 stubToy 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:");

Page 7: assn1

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()*(1-two.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()*(1-three.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()*(1-two.getDiscount())<st.getPrice()*(1-st.getDiscount()))

st=two;if (three.getPrice()*(1-

three.getDiscount())<st.getPrice()*(1-st.getDiscount()))st=three;

if (two.getPrice()*(1-two.getDiscount())<st.getPrice()*(1-st.getDiscount()))

st=four;return st.getName();*/

Page 8: assn1

}

}

class Toy {

private String name;private String category; private double price;private double discount;

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

{// TODO Auto-generated constructor stubthis.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;

}

Page 9: assn1

}