Calling a print method from another class with primitive data types in it
public void printDetails() {
System.out.println("Customer:" + " " +
customer.getFullName());
System.out.println("Vehicle Description:" + " " +
getVehiclePurchased());
PurchaseDate.printPurchaseDate();
}
The basic problem in your code is that your calling the printPurchaseDate() in static way but it is a non static method. You have to create the refrence of PurchaseDate class and calll the method with the refrence
PurchaseDate pd = new PurchaseDate();
public void printDetails() {
System.out.println("Customer:" + " " +
customer.getFullName());
System.out.println("Vehicle Description:" + " " +
getVehiclePurchased());
pd.printPurchaseDate();
}
Other thing you can do it to declair method static.
public static void printPurchaseDate(){
// your code here
}
Stefan Timotic
Updated on June 04, 2022Comments
-
Stefan Timotic almost 2 years
I have been programming in Java using BlueJ for 2 months now and I need some help with an assignment. I am making a basic Vehicle Purchase data entry program. I need to call the
printPurchaseDate()
method from thePurchaseDate
class. The problem I am faced with is that the print statement has three int values: year, month, and day. When I try to call this method in the Vehicle class in theprintDetails()
method, it tells me that I need a return, if I take away the void I have to label the method as a string. However it doesn't work then because it holds three int variables inside it that conflict with the string method. How can I go about doing this? I basically want to print all of my information including thepurchaseDate
. My apologies in advance if I did not present my question properly, this is my first post. Thank you for your help.I have two classes: Purchase Date and Vehicle.
My vehicle class has this method, designed to print out my information:
public void printDetails() { System.out.println("Customer:" + " " + customer.getFullName()); System.out.println("Vehicle Description:" + " " + getVehiclePurchased()); PurchaseDate.printPurchaseDate(); }
Im having issues with printing the date from my PurchaseDate class in my Vehicle class's "printDetails()" method.
/** * The Purchase data class */ public class PurchaseDate { private int year; private int month; private int day; private static final int CURRENT_YEAR = 2014; private static final int LAST_MONTH = 12; private static final int LAST_DAY = 31; /** * default constructor */ public PurchaseDate() { } /** * @param year to initialize theYear field * @param month to initialize theMonth field * @param day to initialize theDay field */ public PurchaseDate(int theYear, int theMonth, int theDay) { setYear(theYear); setMonth(theMonth); setDay(theDay); if (year < 1900 || year > 2014) { System.out.println("The year value can be no greater than the current year"); } else { this.year = year; } if (month < 1 || month > 12) { System.out.println("The month value must be between 1 and 12"); } else { this.month = month; } if (day < 1 || day > 31) { System.out.println("The day value must be between 1 and 31"); } else { this.day = day; } } //Mutators and Accessors /** * @return year */ public int getYear() { return year; } /** * @return month */ public int getMonth() { return month; } /** * @return day */ public int getDay() { return day; } /** * @param the year */ public final void setYear(int newYear) { year = newYear; } /** * @param the month */ public final void setMonth(int newMonth) { month = newMonth; } /** * @param the day */ public final void setDay(int newDay) { day = newDay; } /** * prints the purchase date */ public void printPurchaseDate() { System.out.println("The purchase date is:" + " " + year + "-" + month + "-" + day); } }
I basically want my
System.out.println
to print out the date that I have in myPurchaseDate
class.