Starting from:

$20

CS1331 Homework 1 -Age Calculator Solved


Problem Description
You’ve now been introduced writing a program in Java. This means you can write simple, but useful programs! Given your newfound skills, you’ve been hired by a company to calculate the age of customers! You’ve been provided with a Java class, AgeCalculator that you need to fill in to the appropriate specifications.

Solution Description
For this assignment, you won’t be expected to write your own class. Instead, we’ll provide the class and you simply have to fill in the main method. Keep in mind that what you print out should match the defined output exactly. Deviation from what the output should be, even by a character or two, will result in lost points.

To start, save the following code in a file called AgeCalculator.java:

public class AgeCalculator { public static void main(String[] args) { // Do not modify the following line.

int birthYear = Integer.parseInt(args[0]); // Part 1:

// Part 2:

}

}
 

Part 1: Calculate the age
Inside of the main method, create a variable called age that can hold integers. Assign to age the age of the subject. Assume that someone’s age is the difference between 2019 and their birth year.

Part 2: Report the age
Once you calculate the age, it’s time to print it out. Output should be in the form:

This person is [age] years old!

Testing your app
In order to test your program, you can run java AgeCalculator [birth year] with some example year. For example,

java AgeCalculator 1998 prints out This person is 21 years old!

java AgeCalculator 2001 prints out This person is 18 years old! ___

Allowed Imports

To prevent trivialization of the assignment, you are not allowed to import any classes or packages.

Feature Restrictions
There are a few features and methods in Java that overly simplify the concepts we are trying to teach. For that reason, do not use any of the following in your final submission:

•     var (the reserved keyword)

•     System.exit

Collaboration
Collaboration Statement
To ensure that you acknowledge a collaboration and give credit where credit is due, we require that you place a collaboration statement as a comment at the top of at least one java file that you submit. That collaboration statement should say either:

I worked on the homework assignment alone, using only course materials.

or

In order to help learn course concepts, I worked on the homework with [give the names of the people you worked with], discussed homework topics and issues with [provide names of people], and/or consulted related material that can be found at [cite any other materials not provided as course materials for CS 1331 that assisted your learning].

Recall that comments are special lines in Java that begin with //.

More products