I don’t know how to properly encapsulate a scanner variable. Isn’t the point of encapsulation to keep the variables private? If I have to declare a variable to take the Scanner input, isn’t that technically not secure since it’s a public variable? Wouldn’t it make more sense to have something like: heartRates.setFirstName(scanner.nextLine());? But when I tried to use that instead, I couldn’t figure out how to format them. Sorry if this is a noob question, big noob here.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter your first name: ");
String firstName = scanner.nextLine();
System.out.println("Please enter your last name: ");
String lastName = scanner.nextLine();
System.out.println("Please enter your birth month (1-12): ");
int birthMonth = scanner.nextInt();
System.out.println("Please enter your birth day (1-31): ");
int birthDay = scanner.nextInt();
System.out.println("Please enter the year you were born (yyyy): ");
int birthYear = scanner.nextInt();
HeartRate heartRate = new HeartRate(firstName, lastName, birthMonth, birthDay, birthYear);
heartRate.setFirstName(firstName); // Use setter methods to set the values
heartRate.setLastName(lastName);
heartRate.setBirthMonth(birthMonth);
heartRate.setBirthDay(birthDay);
heartRate.setBirthYear(birthYear);
System.out.println("First Name: "+heartRate.getFirstName());
System.out.println("Last name: "+heartRate.getLastName());
System.out.println("Birth Date: "+heartRate.getBirthMonth()+"/"+heartRate.getBirthDay()+"/"+heartRate.getBirthYear());
System.out.println("Age: "+heartRate.calculateAge());
System.out.println("Max Heart Rate: " +heartRate.calculateMaxHeartRate());
System.out.println("Target Heart Rate: "+heartRate.calculateTargetHeartRate());
}
}
Coffee Soup is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1