Question
Hi, i am having some problem with displaying the Day of Birth in my output. I am not sure what is wrong with my code.
Hi, i am having some problem with displaying the Day of Birth in my output. I am not sure what is wrong with my code.
Below is an example of my code:
import java.util.Scanner; import java.io.IOException; import java.io.File;
enum Month { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec; }
class Date {
private int day; private Month month; private int year;
public Date() {
this(1, Month.Jan, 2021); }
public Date(int day, Month month, int year) {
setDate(day, month, year);
}
public Date(Date date) {
this(date.day, date.month, date.year);
}
public int getDay() {
return day;
}
public Month getMonth() {
return month;
}
public int getYear() {
return year;
}
public void setDate(int day, Month month, int year) {
this.day = day; this.month = month; this.year = year;
}
}
class HeartRates {
private String firstName, lastName; private Date dob; private int currentYear;
public HeartRates() {
}
public HeartRates(String firstName, String lastName, Date dob, int currentYear) {
this.firstName = firstName; this.lastName = lastName; this.dob = dob; this.currentYear = currentYear; }
public HeartRates(HeartRates hr) {
this(hr.firstName, hr.lastName, hr.dob, hr.currentYear);
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public Date getDoB() {
return dob;
}
public int getCurrentYear() {
return currentYear;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setDoB(Date dob) {
this.dob = dob;
}
public void setCurrentYear(int currentYear) {
this.currentYear = currentYear;
}
public int getAge() {
int age = currentYear - dob.getYear();
return age;
}
public int getMaximumHeartRate() {
int maximumHeartRate = 220 - getAge();
return maximumHeartRate;
}
public double getMinimumTargetHeartRate() {
int maximumHeartRate = getMaximumHeartRate(); double minimumTargetHeartRate = maximumHeartRate * 50 / 100;
return minimumTargetHeartRate;
}
public double getMaximumTargetHeartRate() {
int maximumHeartRate = getMaximumHeartRate(); double maximumTargetHeartRate = maximumHeartRate * 85 / 100;
return maximumTargetHeartRate;
}
public void displayInfo() {
System.out.printf("Name: %s %s%n", getFirstName(), getLastName()); System.out.printf("Date of Birth: %s%n", getDoB()); System.out.printf("Current Year: %d%n", getCurrentYear()); System.out.printf("Your age: %d years old%n", getAge()); System.out.printf("%s:%n", "Clinical analysis based on your age"); System.out.printf("\t 1. Your maximum heart rate is: %d%n", getMaximumHeartRate()); System.out.printf("\t 2. Your minimum target heart rate is: %.2f%n", getMinimumTargetHeartRate()); System.out.printf("\t 3. Your maximum target heart rate is: %.2f%n", getMaximumTargetHeartRate()); System.out.printf("%n");
}
}
public class Assignment2_Part_2 {
private static Scanner input;
public static void main(String[] args) throws IOException {
try {
input = new Scanner(new File("personInfo.txt"));
HeartRates heartRates = new HeartRates();
while (input.hasNextLine()) {
heartRates.setFirstName(input.nextLine()); heartRates.setLastName(input.nextLine());
String line = input.nextLine(); String[] dobArr = line.split(" "); Date dob = new Date(Integer.parseInt(dobArr[0]), Month.valueOf(dobArr[1]), Integer.parseInt(dobArr[2]));
heartRates.setDoB(dob);
heartRates.setCurrentYear(input.nextInt());
heartRates.displayInfo();
input.nextLine();
} } catch (Exception e) {
System.out.printf("%s", "There is nothing left to print.");
}
}
I am suppose to display an example of output: Day of Birth: 15 Oct 1988 but instead, i am now currently displaying this output: Day of Birth: D..e@50040f0c.
Inside the personInfo.txt file is as follows:
William
Robert
15 Oct 1988
2018
Richard
Low
13 Jan 1998
2018
Any help would be much appreciated.
Thank you for your time.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started