Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. (Name this program StuPreInputStats) Write a program that takes any number of non-negative integers as input, and output statistics on the data. A


imageimageimageimage

1. (Name this program StuPreInputStats) Write a program that takes any number of non-negative integers as input, and output statistics on the data. A negative integer ends the input and is not included in the statistics. The numbers are ratings on a recently released video game and should be in the range of [0, 5]. Assume there will be at least one rating input. The statistics generated by the program should include the total number of ratings entered, and the number and percentage of each of the following types of ratings: Liked: rating is 4 or 5 Neutral: rating is 3 Disliked: rating is in 0 ~ 2 Invalid: >5. Invalid data may exist due to input errors. Here is a sample run of this program (green italic text indicates user input): Please enter the ratings (end with a negative value): 4 5 1 0 3 4 6 5 4 7 3 4 5 4 2 -2 Total # of ratings: 15 Positive ratings (4-5): 8 (53%) Neutral ratings (3): 2 (13%) Negative ratings (0~2): 3 (20%) Invalid ratings (>5): 2 (13%) Another sample run with data entered one at a line: Please enter the ratings (end with a negative value) : 4 5 1 0 3 4 6 5 4 7 3 4 5 4 2 Total # of ratings: 15 Positive ratings (4-5): 8 (53%) Neutral ratings (3): 2 (13%) Negative ratings (0-2): 3 (20%) Invalid ratings (>5): 2 (13 %) A sample run with a different set of numbers: Please enter the ratings (end with a negative value): 2 1 3 4 -5 Total # of ratings: 4 Positive ratings (4~5): 1 (25%) Neutral ratings (3): 1 (25%) Negative ratings (0-2): 2 (50%) Invalid ratings (>5): 0 (0%) How to Print a Number in Percentage System.out.printf () introduced in Ch2.7 of our textbook can be used to format floating-point values for output. Here is an example segment of code to print out one integer (formatted with format specifier %d) and one percentage (%. Of is to print a floating-point number with no digits after the decimal point. %% is to print out % itself). int count = 4; int total = 15; System.out.printf ("We want to see count %d and percentage %.0f%%. ", count, 100.0* count / total); It will print out: We want to see count 4 and percentage 27%. Here is how it works. 4 divided by 15 is 0.2666.... Since we want to see the value in percentage (26.66...) instead of 0.2666..., we use 100.0* count total. Format specifier %. Of turns 26.66... into 27 and extra digits are dropped after rounding. We have to print % manually. Because % is used by printf () to start a format specifier (%d, %f, ...), we need to use %% to print out the % character itself. See ch9.2 Output Formatting if you want to know more about printf(). How to Build This Program Here is a possible idea to build this program incrementally. Makes sure your program works properly (compiles and generates proper results) at each step before adding more code. Set up a program with an empty main() method. Add data input portion. To test your work, add temporary prints to print out the data so you can verify that the input is done properly. Remove those temporary prints (or at least comment them out) after you're done with this step. Add code to count and print total. Add code to count and print different types of ratings. Add code to print percentages. Once completed, run your program as shown in the three sample runs and include a screenshot of each run (showing execution result) in your assignment report document. This is to remind you that the final version of your program should work as shown in the sample runs, including prompt messages and data formatting. 2. (Name this program StuPre IPv6) Each network device must be assigned an IP address. An IPv6 (IP address version 6) address is represented in the preferred format as eight groups of four hexadecimal digits. A hexadecimal digit may be 0~9 or a ~ f (or A~F). The groups are separated by colons (:). An example of an IPv6 address is: 2001:0db8:85a3:0000:0000:8a2e: 0370: 7001 Write a method to decide whether a string contains a valid IPv6 address in the preferred format (each h represents a hexadecimal digit): hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh For this assignment, your method must loop through individual characters in the string. Approaches using regular expression and/or API classes like Pattern, Matcher will receive zero credit. Start with a method stub like this one. The main() will be used for unit testing (see ch6.6). // Returns true if the string parameter contains a valid IPv6 // address in the preferred format. Otherwise returns false. public static boolean isValidPreferFormat Ipv6 (String address) { return false; } // for unit testing public static void main(String[] args) { System.out.println("Testing started ..."); String str; // store test strings } str = ""; if (isValidPreferFormat Ipv6 (str) != false) // if not the expected result System.out.println("isValidPreferFormat Ipv6 (" + str + ") failed"; str = "0123:4567:89ab: cdef: 0123:4567:89ab:cdef"; if (isValidPrefer Format Ipv6 (str) != true) // if not the expected result System.out.println("(isValidPreferFormat Ipv6 (" + str + ") failed"; // add more testing cases System.out.println("Testing completed."); Here are examples of valid and invalid IPv6 addresses: Valid: 0123:4567:89ab:cdef: 0123:4567:89Ab:cDEf 0000:0000:0000:dddd: eeee :dddd: eeee:ffff 0000:0000:0000:0000:0000:0000:0000:0000 Invalid: 123:4567:89ab:cdef:0123:4567:89ab: cdef0 -> not 4 hex digits in 1st and last groups -> not enough digits/groups -> non-hex digits (in 1st group) 0123:4567:89ab:cdef -> two colons together 0000:0000:0000 01hg: 4567:89ab:cdef: 0123:4567:89ab:cdef 0123:4567::89b:cdef: Use a proper incremental development strategy to build your program. It is a good idea to first check whether the parameter string is of the right length. Following the unit testing examples and add at least 3 more testing cases of your choice to your main(): 1 valid address and 2 invalid ones. When you are done, include a screenshot of the execution of your program in your assignment report document. If it works properly, your program should only print those two lines: Testing started ... Testing completed.

Step by Step Solution

3.42 Rating (152 Votes )

There are 3 Steps involved in it

Step: 1

For the first task Ill provide a Java program named StuPreInputStats that takes nonnegative integers as input calculates statistics and outputs the re... 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

Data Structures and Algorithm Analysis in Java

Authors: Mark A. Weiss

3rd edition

132576279, 978-0132576277

More Books

Students also viewed these Programming questions

Question

Compare and contrast life- cycle pricing and target pricing.

Answered: 1 week ago

Question

=+b) What is the factor?

Answered: 1 week ago

Question

1-What is the purpose of financial planning?

Answered: 1 week ago