Java program called checkstring

Assignment Help Basic Computer Science
Reference no: EM131480586

1. Write a complete Java program called CheckString according to the following guidelines.

** Each method below, including main, should handle (catch) any Exceptions that are thrown. **

** If an Exception is thrown and caught, print the Exception's message to the command line. **

You main method should do the following in the order specified, where the methods referred to are further described below:

1. Call the getWord method and print the result to the command line.

2. Create an array of Strings called testInputData and populate it with at least three elements.

3. Call the writeTextFile method above, passing the array you created in exercise step 2 above and the String "data.txt".

4. Call the readTextFile method to read the file you wrote in step 3 above. Assign the result of readTextFile to an ArrayList variable in main called fileContents.

5. Write a loop to print the contents of the fileContentsArrayList to the command line.

The getWord method takes no parameters and returns a String. This method prompts the user for a word, and then calls the checkWord method (see below), passing as a parameter the word the user provided as input. Make sure the getWord method handles the Exception that may be thrown by the checkWord method.

The checkWord method takes a String parameter named word, returns nothing, and is declared to throw an Exception of type Exception. In the method, check if the first character of the parameter is a letter. If it is not a letter, the method throws an Exception of type Exception with the message of: "This is an invalid word entry."

The writeTextFile method takes two parameters: an array of Strings (named arrayToWrite) and a String (named inputFilename). The method writes the Strings in the arrayToWrite array to a text file named filename (the parameter), with each String on a separate line.

The readTextFile method takes a String as a parameter (filename) and returns an ArrayList of Strings (fileContents). The method reads the text file identified by the filename parameter and populates the ArrayList with an element for each line in the text file.

2. Write a complete Java program called CalcWeightedAvgDropLowest according to the following guidelines.

For this program, instead of getting the data from the user via System.in, get only input and output file names from the user. See Horstmann's program Total.java in Section 7.1, pp. 319, 320, on which this assignment is based.

Also note that the methods for getting input and writing output will need to handle exceptions, whereas in Horstmann's Total.java example, only the main needs to handle exceptions. The simplest (and recommended) way to handle exceptions in this program is to have the two methods that read and write data each throw a FileNotFoundException. We'll see in a later program how to handle exceptions differently.

Input File

The input file - which you need to create and prompt the user for the name of - should be called 'data.txt', and it should be created according to the instructions below.

The input file should contain (in order): the weight (greater than 0 and less than or equal to 1), the number, n, of lowest numbers to drop, and the numbers to be averaged after dropping the lowest n values.

For example, you might have these values in your input file (data.txt):

0.5 3 10 70 90 80 20

Creating the Input file

To create the input file, while in NetBeans with your project open, click to highlight the top-level folder of your project in the projects tab at the upper left. Then starting with the NetBeans File menu, do

File->New File

Keep the Project name at the top; keep Filter blank

For Categories choose Other (at the bottom of the categories list)

For File Types choose Empty File (at the bottom of the files list)

Next

FileName: data.txt

Folder: this should be blank; if it's not, delete whatever's there.

Finish

In the empty file data.txt that you just created, add a single line of data like the one in the example above, where the weight is a double (greater than 0.0 and less than or equal to 1.0) and the other numbers are the number, n, of lowest values to drop, and then the numbers to be averaged after dropping the lowest n values. Then save this data.txt input file.

It's important that your input file is where NetBeans will look to find it. The above instructions should make sure that that happens.

Main method

Your main method should contain just three lines like these:

ArrayListinputValues = getData();

double weightedAvg = calcWeightedAvg(inputValues);

printResults(inputValues, weightedAvg);

where the printResults method should prompt the user for the name of the output file and print to that output file.

Output

Given the sample values above in the data.txt input file, the output file should contain something very much like the following:

The weighted average of the numbers is 42.5, when using the data 10.0, 70.0, 90.0, 80.0, 20.0, where 0.5 is the weight used, and the average is computed after dropping the lowest 3 values.

Prompting for names of input file and output file

Make sure you prompt the user for the name of the input file (even though we know what it is). You can use a prompt like: "Enter "data.txt" (no quotes) for the name of the input file: "

Also prompt the user for the name of the output file. If you use code like that in Horstmann's Total.java program to create the output file, the output file should be located at the same place as the input file. Check the NetBeans Files tab (next to the Project tab in the upper left) to see both the input and output files.

3. Create a program called CalcWeightedAvgWithExceptions, according to the following guidelines and using try-catch-finally blocks in your methods that read from a file and write to a file, as in the examples in the lesson notes for reading and writing text files.

Input File

The input file - which you need to create and prompt the user for the name of - should be called 'data.txt', and it should be created according to the instructions below.

The input file should contain (in order): the weight (greater than 0 and less than or equal to 1), the number, n, of lowest numbers to drop, and the numbers to be averaged after dropping the lowest n values.

For example, you might have these values in your input file (data.txt):

0.5 3 10 70 90 80 20

Creating the Input file

To create the input file, while in NetBeans with your project open, click to highlight the top-level folder of your project in the projects tab at the upper left. Then starting with the NetBeans File menu, do

File->New File

Keep the Project name at the top; keep Filter blank

For Categories choose Other (at the bottom of the categories list)

For File Types choose Empty File (at the bottom of the files list)

Next

FileName: data.txt

Folder: this should be blank; if it's not, delete whatever's there.

Finish

In the empty file data.txt that you just created, add a single line of data like the one in the example above, where the weight is a double (greater than 0.0 and less than or equal to 1.0) and the other numbers are the number, n, of lowest values to drop, and then the numbers to be averaged after dropping the lowest n values. Then save this data.txt input file.

It's important that your input file is where NetBeans will look to find it. The above instructions should make sure that that happens.

main method

Your main method should contain just three lines like these:

ArrayListinputValues = getData();

double weightedAvg = calcWeightedAvg(inputValues);

printResults(inputValues, weightedAvg);

where the printResults method should prompt the user for the name of the output file and print to that output file.

Output

Given the sample values above in the data.txt input file, the output file should contain something very much like the following:

The weighted average of the numbers is 42.5, when using the data 10.0, 70.0, 90.0, 80.0, 20.0, where 0.5 is the weight used, and the average is computed after dropping the lowest 3 values.

Prompting for names of input file and output file

Make sure you prompt the user for the name of the input file (even though we know what it is). You can use a prompt like: "Enter "data.txt" (no quotes) for the name of the input file: "

Also prompt the user for the name of the output file. If you use code like that in Horstmann's Total.java program to create the output file, the output file should be located at the same place as the input file. Check the NetBeans Files tab (next to the Project tab in the upper left) to see both the input and output files.

Reference no: EM131480586

Questions Cloud

Positive-length intervals : Recall that in the Activity Selection problem we are given it positive-length intervals beginning and ending at integer values
Mistakes of web-based presentation of information : Pick a website and evaluate how well it avoids the top ten mistakes of web-based presentation of information as described in the text (Tullis, 2005)
Consider an arp poisoning attack : Consider an ARP poisoning attack. What information would you expect to be collected in an auditing tool? What purpose would that information serve?
Define strategic issues and challenges : Define strategic issues and challenges. define current strategy; delineate competitive advantage;? perform SWOT analysis.
Java program called checkstring : 1. Write a complete Java program called CheckString according to the following guidelines.
The type of treatment facility your team : As a member of this team, you will need to choose 1 treatment model used in the type of treatment facility your team has decided to recommend.
Plot the production possibilities frontier : The following table shows scores that a student can earn on two upcoming exams according to the amount of time devoted to study.
What is difference between team and multiparty negotiations : What is the difference between team and multiparty negotiations? What are the advantages and disadvantages of each? When would you use one type over the other?
Interface of a device : Write a short critique (3-4 paragraphs) of the interface of a device, a web site, or a piece of software. Provide a picture or a sketch that illustrates.

Reviews

Write a Review

Basic Computer Science Questions & Answers

  Identifies the cost of computer

identifies the cost of computer components to configure a computer system (including all peripheral devices where needed) for use in one of the following four situations:

  Input devices

Compare how the gestures data is generated and represented for interpretation in each of the following input devices. In your comparison, consider the data formats (radio waves, electrical signal, sound, etc.), device drivers, operating systems suppo..

  Cores on computer systems

Assignment : Cores on Computer Systems:  Differentiate between multiprocessor systems and many-core systems in terms of power efficiency, cost benefit analysis, instructions processing efficiency, and packaging form factors.

  Prepare an annual budget in an excel spreadsheet

Prepare working solutions in Excel that will manage the annual budget

  Write a research paper in relation to a software design

Research paper in relation to a Software Design related topic

  Describe the forest, domain, ou, and trust configuration

Describe the forest, domain, OU, and trust configuration for Bluesky. Include a chart or diagram of the current configuration. Currently Bluesky has a single domain and default OU structure.

  Construct a truth table for the boolean expression

Construct a truth table for the Boolean expressions ABC + A'B'C' ABC + AB'C' + A'B'C' A(BC' + B'C)

  Evaluate the cost of materials

Evaluate the cost of materials

  The marie simulator

Depending on how comfortable you are with using the MARIE simulator after reading

  What is the main advantage of using master pages

What is the main advantage of using master pages. Explain the purpose and advantage of using styles.

  Describe the three fundamental models of distributed systems

Explain the two approaches to packet delivery by the network layer in Distributed Systems. Describe the three fundamental models of Distributed Systems

  Distinguish between caching and buffering

Distinguish between caching and buffering The failure model defines the ways in which failure may occur in order to provide an understanding of the effects of failure. Give one type of failure with a brief description of the failure

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd