Java Program, Having Two Problems, Tostringmethod() Method And Appropriate Calculations? Help!?

public class NewCar
{
//attributes
String year, make, model, description, abbreviation, abbrYear;
double stickerPrice, discount, salesTax;
char abbrMake, abbrModel;
String abbrMakeS, abbrModelS;//responsible for the conversion from char to String
double calcFinalPrice;//compute and calculate the final number based on stickerPrice, discount, and salesTax
int calcMonthlyPayment, numOfMonthsAtZeroPctInterest;//compute and calculate the number of months with zero percent interest
//default constructor
public NewCar()
{
String description = “2010 Chevy Cobalt”;
String abbrYear = description.substring(2,4);//last two letters of the year ’10’
char abbrMake = description.charAt(5);//first letter of the make ‘C’
char abbrModel = description.charAt(11);//first letter of the model ‘C’
}
//parameterized constructor
public NewCar(String _year, String _make, String _model)
{
_year = null;
_make = null;
_model = null;
}
public void calcFinalPrice(double _stickerPrice, double _discount, double _salesTax)
{
stickerPrice = _stickerPrice;
discount = _discount;
salesTax = _salesTax;
calcFinalPrice = stickerPrice – discount + salesTax;//calcFinalPrice calculation with instance variables, wrong?
}
public double getCalcFinalPrice()
{
return calcFinalPrice;
}
public void calcZeroPctMonPayt(int _numOfMonthsAtZeroPctInterest)
{
numOfMonthsAtZeroPctInterest = _numOfMonthsAtZeroPctInterest;
calcMonthlyPayment = numOfMonthsAtZeroPctInterest;//calculati… wrong!!!!! fix this….
}
public int getCalcMonthlyPayment()
{
return calcMonthlyPayment;
}
public void toStringMethod()
{
abbrMakeS = Character.toString(abbrMake);//converts from char to String, abbrMakeS is now a string
abbrModelS = Character.toString(abbrModel);//converts from char to String, abbrModelS is now a string
abbreviation = abbrYear + abbrMakeS + abbrModelS;//complete string conversion together applied to abbreviation
}
public String getAbbreviation()
{
return abbreviation;
}
}
TEST:
import java.util.Scanner;
public class NewCarTester
{
public static void main(String [] args)
{
NewCar nc = new NewCar(“2010”, “Honda”, “Civic”);
Scanner scn = new Scanner(System.in);
System.out.println(“Enter the car’s Year, Make and Model: “);
String description = scn.nextLine();
System.out.println(“Enter the Sticker Price: “);
double stickerPrice = scn.nextDouble();
System.out.println(“Enter the Discount: “);
double discount = scn.nextDouble();
System.out.println(“Enter the Sales Tax Rate: “);
double salesTax = scn.nextDouble();
nc.calcFinalPrice(stickerPrice, discount, salesTax);//mutator method invoked for finalPrice
System.out.println(“Enter the Number of Months at Zero Percent Interest: “);
int monthsOfZeroPercentInterest = scn.nextInt();
nc.calcZeroPctMonPayt(monthsOfZeroPe… method invoked for monthsOfZeroPercentInterest
nc.toStringMethod();//mutator method for conversion from char to string as well as user input from scanner
System.out.println(“You want to purchase a \”” + description + “\” Abbreviation: \”” + nc.getAbbreviation() + “\””);
System.out.println(“Final price: ” + nc.getCalcFinalPrice());
System.out.println(“Monthly Payment at zero percent: ” + nc.getCalcMonthlyPayment());
}
}
OUTPUT SUPPOSE TO BE:
Enter the car’s Year, Make and Model: 2010 Honda Civic
Enter the Sticker Price: 22000
Enter the Discount: 2000
Enter the Sales Tax Rate: .08
Enter the Number of Months at Zero Percent Interest: 36
You want to purchase a “2010 Honda Civic” Abbreviation: “10HC”
Final Price: 21600.00
Monthly Payment at zero percent: 600.00
PROBLEM:
The first problem I have is I’m not sure how to approach the toStringMethod() method to produce “10HC” with the combined attributes without parameters in the test class. (NOTE: I was thinking it needs parameters in order for Scanner to recognize theses input variables and print the output from the class.)
The second problem is the calculation is wrong and I’m not good at math. My homework didnt give me the calculation or equation to how to do the numbers in, so I need the calculation for calcMonthlyPayment and numOfMonthsAtZeroPctInterest.
Thanks, much appreciated! 🙂

Both comments and pings are currently closed.

One Response to “Java Program, Having Two Problems, Tostringmethod() Method And Appropriate Calculations? Help!?”

  1. HMJava says:

    First of all
    public void toStringMethod()
    {
    abbrMakeS = Character.toString(abbrMake);//converts from char to String, abbrMakeS is now a string
    abbrModelS = Character.toString(abbrModel);//converts from char to String, abbrModelS is now a string
    abbreviation = abbrYear + abbrMakeS + abbrModelS;//complete string conversion together applied to abbreviation
    }
    should be
    @Override
    public String toString()
    {
    abbrMakeS = Character.toString(abbrMake);//converts from char to String, abbrMakeS is now a string
    abbrModelS = Character.toString(abbrModel);//converts from char to String, abbrModelS is now a string
    abbreviation = abbrYear + abbrMakeS + abbrModelS;//complete string conversion together applied to abbreviation
    return abbreviation; //to string must return a string
    }

Powered by WordPress | Designed by: free css template | Thanks to hostgator coupon and web hosting reviews