Calling a print method from another class with primitive data types in it
I have been programming in Java using BlueJ for 2 months and I need help with the assignment. I am doing a basic car purchase data entry program. I need to call a method printPurchaseDate()
from a class PurchaseDate
. The problem I am facing is that the print statement has three int values: year, month and day. When I try to call this method on the Vehicle class in the method printDetails()
, it tells me I need a return, if I pick up the void, I have to mark this method as a string. However, this doesn't work because it contains three intranet variables that conflict with the string method. How can i do this? I basically want to print all my information including PurchaseDate
. My apologies in advance if I did not present my question correctly, this is my first post. Thank you for your help.
I have two classes: date of purchase and vehicle.
My car class has this method designed to print my information:
public void printDetails() {
System.out.println("Customer:" + " " +
customer.getFullName());
System.out.println("Vehicle Description:" + " " +
getVehiclePurchased());
PurchaseDate.printPurchaseDate();
}
I am having problems printing the date from my PurchaseDate class in the "printDetails ()" method of my class.
/**
* 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);
}
}
Basically I want mine to System.out.println
print out the date that I have in class PurchaseDate
.
source to share
public void printDetails() {
System.out.println("Customer:" + " " +
customer.getFullName());
System.out.println("Vehicle Description:" + " " +
getVehiclePurchased());
PurchaseDate.printPurchaseDate();
}
The main problem with your code is that you are calling printPurchaseDate () in a static way, but it is not a static method. You have to create a refrence of the PurchaseDate class and call the method with refrence
PurchaseDate pd = new PurchaseDate();
public void printDetails() {
System.out.println("Customer:" + " " +
customer.getFullName());
System.out.println("Vehicle Description:" + " " +
getVehiclePurchased());
pd.printPurchaseDate();
}
Another thing is that you can do this to declare the method static.
public static void printPurchaseDate(){
// your code here
}
source to share
Your method printPurchaseDate()
returns void
and println()
expects something (i.e. a string). To fix this,
public String printPurchaseDate() {
return "The purchase date is: " + String.valueOf(year) + "-" + String.valueOf(month) + "-" + String.valueOf(day);
}
This should do the trick.
source to share
Any of these methods will solve your problem:
public String getPurchaseDate() {
return "The purchase date is:" + " " + year + "-" + month + "-" + day;
}
public void printDetails() {
System.out.println("Customer:" + " " +
customer.getFullName());
System.out.println("Vehicle Description:" + " " +
getVehiclePurchased());
System.out.println(PurchaseDate.getPurchaseDate());
}
OR
public void printPurchaseDate() {
System.out.println("The purchase date is:" + " " + year + "-" + month + "-" + day);
}
public void printDetails() {
System.out.println("Customer:" + " " +
customer.getFullName());
System.out.println("Vehicle Description:" + " " +
getVehiclePurchased());
PurchaseDate.printPurchaseDate();
}
source to share