Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

please check this program

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//super class
package myownautoshopproject;



public class Car {

private int speed;

private double regularPrice;

private String color;

/** Creates a new instance of Car */
public Car(){

}

public Car(double regularPrice, int speed, String color) {
this.regularPrice = regularPrice;
this.speed = speed;
this.color = color;
}


//This method will be over riden in the sub classes to get specific behavior...
public double getSalePrice() {
return 0;
}

public double getRegularPrice(){
return regularPrice;
}
}

// child class

package myownautoshopproject;

public class Ford extends Car {

private int year;
private int manufacturerDiscount;

/** Creates a new instance of Ford */
public Ford(double regularPrice, int speed, String color, int year, int manufacturerDiscount) {
super( regularPrice, speed, color);
this.year = year;
this.manufacturerDiscount = manufacturerDiscount;
}

public double getSalePrice() {
System.out.println( (float)(10/100) );
System.out.println(getManufacturerDiscount() );
return ( getRegularPrice() - (getRegularPrice() * getManufacturerDiscount( ) ) );
}

public double getManufacturerDiscount(){
double d = (double)( manufacturerDiscount / 100 );
return d;
}


}

//Main class

package myownautoshopproject;


public class Main {


/**
* @param args the command line arguments
*/
public static void main(String[] args) {

Car[] car = new Car[4];

car[0] = new Car();
car[1] = new Ford(5000,120,"Black",1997,20);
car[2] = new Ford(8000,180,"Green",2007,15);
car[3] = new Sedan(10000,250,"Bule",25);



for(int i = 0; i < car.length; i++){

//totalRegularPrice += pa[i].getRegularPrice();

//totalSalePrice += pa[i].computeSalePrice();

System.out.println("Item number " + i +
": Type = " + car[i].getClass().getName() +
", Regular price = " + car[i].getRegularPrice() +
", Sale price = " + car[i].getSalePrice() );

}


}

}


The problem is after initialising the manufacturerDiscount field through Ford class constructor, when i want to convert it to a percentage....it becomes 0.0 and consequently there is no manufacturer discount to deduct from regular price in the getSalePrice() method of the Ford class....please help me....thank you...
 
hem kum
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please comment out the first line in getSalePrice() of the Ford class....Thank you..
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try converting to double first and then divide by 100 :roll:
 
hem kum
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks rakesh ..you are correct...and can you please tell me in the below program how to print the correct value of 0.15....as it is printing only 0 .


public class test {

public static void main(String args[]){

//int n = 500;

//float p = 15/100;
System.out.println(15/100);
}

}
 
rakesh sugirtharaj
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try

((float)15)/100..

The thing is, normal calculations like 15/100 are taken as integer..
15/100 gives 0.15 and it takes only the 0 because its getting converted to int..
 
hem kum
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ohh thanks rakesh for your helping...now all these things are working...
 
Well behaved women rarely make history - Eleanor Roosevelt. tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic