Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Problem: 1) Class: ScannerPractice Create the class ScannerPractice. It must contain the following implemented methods, though you can add more helper methods (make them

Java Problem:

1) Class: ScannerPractice

Create the class ScannerPractice. It must contain the following implemented methods, though you can add more helper methods (make them public static ... and you can call them from our others).

sumReals(Scanner)

public static double sumReals(Scanner input)

This method takes an already open Scanner and proceeds to read input words from it. If any input word happens to represent a valid real number, it is added to a running total. When no input remains in the Scanner, this running total is returned. If no words are valid real numbers then 0.0 should be returned. Some example uses in DrJava's interactive loop are shown below.

Welcome to DrJava. > import java.util.Scanner; // All numbers > String allNums = "1.23 4.56 7.89"; > Scanner in = new Scanner(allNums); > double total = ScannerPractice.sumReals(in); > total 13.68 // All words > String allWords = "none of these are numbers"; > in = new Scanner(allWords); > ScannerPractice.sumReals(in) 0.0 // Mixed words and real numbers > String mixed = "number 1.0 two 12.3 stuffed turkey -0.99 five"; > in = new Scanner(mixed); > ScannerPractice.sumReals(in) 12.31 

Constraint: Do not use regular expressions to solve this problem. They are not needed and will only over-complicate a good solution. Instead, focus on the built-in ability of Scanner to recognize different kinds of input and distinguish when a number can be read next or if the next token is an arbitrary string.

sumReals(String)

public static double sumReals(String parseString)

Rather than take an already opened Scanner, this method takes a String to parse. It should create a Scanner object that reads from the parameter String and then perform the same computation as sumReals(Scanner).

Note that this illustrates that Java methods may have the same name, as long as they have unique sequences of parameter types.

Some sample uses:

> ScannerPractice.sumReals("here are some numbers... psyche!") 0.0 > ScannerPractice.sumReals("43110") 43110.0 > ScannerPractice.sumReals("4.3 110.5 0.12 3.14") 118.06 > ScannerPractice.sumReals("one 1.0 two 2.0 thre-and-a-half 3.5 and so on... ") 6.5 

Hint: It is tempting to copy and paste code for similar methods but clever programmers figure out how to use an already written method to solve a new problem by passing the old method appropriately constructed arguments. The best solutions to this problem are 1-2 lines long.

sumRealsInFile(String filename)

public static double sumRealsInFile(String filename) throws Exception

This method opens a pre-existing file on disk, reads it word by word and sums any valid real numbers that appear in it. Some example files are present with the code distribution called sample1.txt and sample2.txt. Examples of summing reals in these files are as follows.

> ScannerPractice.sumRealsInFile("sample1.txt") 15.912099999999999 > ScannerPractice.sumRealsInFile("sample2.txt") -12.3 

Hint: The best solutions to this problem are 1-2 lines long and make use of another method you wrote.

Notice that the type signature for the method includes throws Exception. In Java, some operations can go wrong which triggers an exception. Methods that can cause certain kinds of exceptions must "admit" this fact by declaring that they can throw some kind of exception. In this case, creating a Scanner that reads from a file may encounter difficulties. For instance, if the requested file is not present, then an exception will be raised. For now, we will simply state an arbitrary Exception may be thrown but we can do better by listing the specific kinds of exceptions that can be thrown (in this case a FileNotFoundException).

Note - only "checked" exceptions must have these throws annotations added; RuntimeException and Error types and their descendents are unchecked, and all others are checked. Most exceptions you experience will be either a RuntimeException (unchecked) or an IOException (checked), as we see here.

-----------------------------

2) class Date

Create the class Date. It represents a day in the Gregorian calendar (in a pretty simplfied representation).

Instance Variables

Each object of this class will have its own copy of each instance variable. We sometimes call them "fields" (focusing on how they store a value) or "members" (focusing on how they are a definition inside a class, which is also true of methods).

public int year, month, day

Assume that year is positive; that month is a value from 1 to 12 inclusive; and that day is a valid day in the indicated month.

Methods

public Date(int year, int month, int day)

This constructor method assigns the given actual parameters to the corresponding instance variables.

public String toString()

Much like Python's __str__ method, this method returns a String representation of the object. For this class, we require strings that follow the obvious pattern in these examples:

"2018-02-28" "2000-02-29" "1949-10-01" 

-----------------------------

3) Class: Person

Create the class Person. It must contain the following definitions, but you are welcome to add more if you'd like.

Instance Variables

Each object of this class will have its own copy of each instance variable. We sometimes call them "fields" (focusing on how they store a value) or "members" (focusing on how they are a definition inside a class, which is also true of methods).

Include these instance variable definitions:

public String name

public Date birthday

public boolean isHappy

Methods

public Person (String name, Date birthday, boolean isHappy) This constructor method must accept the three actual parameters and store them to the indicated instance variables.

public int age(Date today) Finds and returns what the person's age is today. Don't worry about validating dates, just compare this person's birthday to today and figure out how many full years they've completed, and return that integer.

public void seePuppy() This method makes the person happy.

public boolean canRentCar(Date today) Our fictitious car rental company, Budgamoprise, only rents cars to people who are 25 and older. This method indicates (returns) if a person is old enough to rent a car from Budgamoprise.

public String toString() Much like Python's __str__ method, this method returns a String representation of the object. For this class, we require strings that follow the obvious pattern in these examples:

"Person(Oscar the Grouch, 1969-11-10, false)" "Person(Groucho Marx, 1890-10-2, true)" 

Note there is a space after each comma, and no quotes in the generated string.

----------------------Tester: https://paste.ee/p/Af82v

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