Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use the following Coding Guidelines: When declaring a variable, you usually want to initialize it. Remember you cannot initialize a number with a string. Remember

Use the following Coding Guidelines: When declaring a variable, you usually want to initialize it. Remember you cannot initialize a number with a string. Remember variable names are case sensitive. Use tabs or spaces to indent code within blocks (code surrounded by braces). Use white space to make your program more readable. Use comments after the ending brace of classes, methods, and blocks to identify to which block it belongs. Problem Description: For this Lab you have to implement a class Person. A person has a firstname, lastname, and birthday year. Supply a constructor and the following methods: getFirstName(), getLastName(), getAge(). Step 1: Getting Started Create a class called Lab6. Be sure to name your file Lab6.java 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: your own description of the program. // FOR: CSE 110- Lab #6 // TIME SPENT: how long it took you to complete the assignment. //-----------------------------------------------------------*/ Step 2: Declaring Class Examining the problem, we need to create a Person class, declare Person class as follows public class Person{ } Inside the class, declare some data members: a string variable called firstname, a string variable called lastname, and an integer called birthYear. // declare some variables of different types: // an string called firstname //--> // a string called lastname //--> // an int called birthYear //--> Step 3: Defining the constructor: Remember the constructor assigns input data to the data members. public Person (String fname, String lname, int year) { // write the segment of code //that assigns input data to the data members } Step4: Supply the methods A method called getFirstName() to get the first name of an object Person: public String getFirstName() { // write a line of code //that returns the first name } A method called getLastName() to get the last name of an object Person: public String getLastName() { //write a line of code that returns last name } A method called getAge() to get the age of an object Person: public int getAge(int currentYear) { // write a segment of code //that returns the age } Step 5: Calling the constructor of Person class from the main method. In order to use Person class variables and methods we need to have a runner function. For any class in java, main method will be the running method. Your class Lab6 will have the main method. In order to use the Person class variables and methods, you need to use the constructor to create an object Person. public class Lab6 { public static void main(String[] args) { //declare variables where you will store //inputs from user --> // declare a Scanner object --> //prompt the user for inputs //firstname, lastname, birthyear --> // store the input in the declared variables --> //use the constructor //to create a brand-new object Person --> } } Hint: Do not forget to import the Scanner package at the very top of your program: import java.util.Scanner; Step 5: Calling Person class methods and display the output The methods in the Person class will display the required outputs. //Call the getFirstName() and getLastName() methods in order to print the name of the object Person you just created. //Call the getAge(currentYear) method in order to print the age. --> System.out.println( is years old in 2016 and will be years old in ten years.) If your PersonObject is person, then this statement will look like. System.out.println(person.getFirstName() + person.getLastName() + is + person.getAge(2016) + years old in 2016 and will be + person.getAge(2026) + years old in ten years ); Step 7: Display the output See the sample output below and make sure to display is the same format. Sample Output: Below is an example of what your output should roughly look like when this lab is completed. All text in bold represents user input 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 24 years old in 2016 and will be 34 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 34 in 2016 and will be 44 in ten years. Last Step: Submit your lab by following the instructions below: ********************************************************************************* Submit your Lab6.java and Person.java file to the Submission Server. Go to the Submission Server site, https://courses.eas.asu.edu/cse110/, then click on Lab Submissions in the left frame. Click on the browse button and find where you saved your Lab6.java file and Person.java on your computer. Upload the files to the site and then click on the Submit button. Your file will be submitted and a screen will show up displaying if your program compiled and what your output is when run on some sample input (in this case nothing). You should then check to make sure that the actual file submitted properly and is readable to the grader. To do so click on Grades in the frame on the left of the page and then click on the 0 underneath Lab6. You will again see that your program compiled and the sample output, but you should scroll down to the bottom of the screen and make sure your file is readable as well.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Students also viewed these Databases questions

Question

=+ How would you advise those problems be resolved?

Answered: 1 week ago