Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Step 1: Getting Started Create a class called Lab6. Use the same setup for setting up your class and main method as you did in

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Step 1: Getting Started Create a class called Lab6. Use the same setup for setting up your class and main method as you did in previous labs and assignments. Be sure to name your file Lab6.java Assignment Documentation At the beginning of each programming assignment you must have a comment block with the following information /AUTHOR: your name //FILENAME: title of the source file //SPECIFICATION: description of the program // FOR: CSE 110- Lab #6 /TIME SPENT: how long it took you to complete the assignment Step 2: Defining a Class Examining the problem, we need to create a Person class. We can define a Person class as follows public class Person nside the Person class definition, declare some instance members: a String variable called firstName, a String variable called lastName, and an int called birthYear // declare variables // an string called firstName // a string called lastName // an int called birthYear Step 3: Implement a Constructor Remember that a constructor initialize an object to a valid state by assigning valid values to the object's variables. Also remember that a constructor method has the same name as the class. public Person (String fname, String Lname, int year) // write the code to initialize the object's varibales / with the values passe in as arguments Step 4: Implement the Methods Implement a method called getFirstName0 that will return the first name of a Person public String getFirstName // return the first name Implement a method called getLastName() that will return the last name of a Person public String getlastName Ireturn the last name Implement a method called getAge) that will return the age of a Person in a given year. public int getAgeCint currentYear) // compute the Person's age in the given currentYear //based on their birthYear // then return that computed age Step 5: Declaring a Person Variable and Instantiating a Person Object Your Lab6 class will have the typical main method. In this main method you will need to call the Person constructor to create a new Person object public class Lab6 t public static void main(String args) // declare a Scanner object // declare variables where you will store inputs from user // for the person's first and last names and their birth year // prompt the user for inputs: firstName, lastName, birthYear I- // store the input in the declared variables // declare a Person variable and call the Person constructor to create a brand-new Person object Hint: Do not forget to import the Scanner from the java.util package at the very top of your program: import java.util.Scanner; Step 6: Calling Person Methods and Displaying Output The methods in the Person class will return the required outputs-which we must print out using System.out.printin or (print or printf) 11 Call the getFirstName and getLastName methods // in order to get and then print the name of the object Person you just created // Call the getAgeCcurrentYear) method in order to get and then print /7 their age this year, and their age ten years from now. Sample Output Below is an example of what your output should roughly look like when this lab is completed, with user input in red. Sample Run 1: Enter the first name of the person: John Enter the last name of the person: Mann Enter the birth year of the person: 1991 John Mann is 28 years old in 2019 and will be 38 years old in ten years Sample Run 2: Enter the first name of the person: Ben Enter the last name of the person: Freeman Enter the birth year of the person: 1981 Ben Freeman is 38 in 2019 and will be 48 in ten years

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions