Java | Computer Science homework help

Category: Computer Science

Hey i have attached the required file for my assignment.

and additionally i have also mentioned the points of assignments below.

this one is based on this concept.

Lab 4 (Collection Framework, Exception,

IO Handling, Generic method and GUI Programming)

  

NOTE: You should properly format your source code files for better readability and write appropriate comments in the code. 

Exercise 1 [10 points]: 

Write a program that asks a user for a file name and prints the number of characters, words (separated by whitespace), and lines in that file. Then, it prints the frequency of each word (case insensitive) containing in the file (Test1.txt); Your program should handle exceptions related to file opening and reading from the file. 

This is a test this 

Hi there hi hi hi 

Output at the console: 

Number of characters: 35 

Number of words: 10 

Number of lines: 2 

Data written to the file (Test1.txt): 

 this  2 

 is  1 

 a  1 

 test  1 

 hi  4 

 there  1 

Hint: you may use a HashMap to store and count the frequency of the words in the file. 

Exercise 2 [20 points]: 

Your task is to write a program that uses a textfile containing any number of lines of double values as input. Each line in the file is supposed to have no more than 15 values on it. If the file exists and each line in the file does in fact contain 15 or fewer double values, then the program processes and reports on each line in turn by displaying its line number, the number of values on that line, and the range of values on the line. 

But, as you know, things are not always as they seem, or as they are supposed to be. For example, the file your program is looking for may not be there, there may be no double values at all on some lines, some lines may contain more values than the allowed maximum of 15, and some lines may contain “values” that aren’t even doubles (i.e., badly formatted “invalid” double values). The program must recognize and respond appropriately to each of these problems. 

Note: You must implement exception handling. Not only am I requiring it, but Java itself will also require it since you will have to use Java resources that force you to deal with the IOException. In addition to this exception, you are also required to handle the ArrayIndexOutOfBounds and the NumberFormatException at appropriate points in your program. 

This program reads in, from the command line, the name of a textfile containing any number of lines of doubles and then processes each line in turn. Processing a line means computing and then reporting, for that line, its line number, along with the range of values on the line. 

The program can recognize a number of problems that may occur when trying to process such a file. First, the file itself may not be present. If so, the following message is displayed (the first line comes from the system, the second from the program itself): 

filename.ext (The system cannot find the file specified) Program will now terminate. 

If the file exists, and contains lines of double values, possibly with blank lines, possibly lines with too many values, and possibly lines with invalid doubles on them, then the following messages, each followed by a pause, are output, as appropriate: 

Line #: Number of values = # and value range = [#, #]. 

Line #: This is a blank line. 

Line #: This line contains more than the maximum of 15 allowed data values. Line #: This line contains an invalid double value. 

For example, if a file name “text.txt” contains the following texts: 

1.0 2.2 30.3 

3.0 4.12 5.25 2 3.3 4.4 5s 6.5 7 abs sej jdh& 4 

1.1 2.2 3.4 4.3 5.5 6.6 6.5 6.5 8.2 99.2 0.0 0.0 -1.1 

 34.2 34.0 23.1 

24.1 

1.1 2.2 3.3 4.4 5.5 6.0 7.7 8.0 9.0 10.1 -2.1 3.2 4.3 5.4 6.5 7.6 

1.0 

Output: 

Then the output should be like this: 

The file test.txt was successfully opened. 

The data in it will now be processed. 

Press Enter to continue … 

Line 1:Number of values = 3 and value range is [1.0, 30.3]. Line 2:This is a blank line. 

Line 3:Number of values = 3 and value range is [3.0, 5.25]. 

Line 4:This line contains an invalid double value. 

Line 5:This line contains an invalid double value. 

Line 6:Number of values = 13 and value range is [-1.1, 99.2]. Line 7:Number of values = 3 and value range is [23.1, 34.2]. Line 8:Number of values = 1 and value range value range consists of this single value. 

Line 9:This line contains more than the maximum of 15 allowed data values. 

Line 10:Number of values = 1 and value range value range consists of this single value. 

  

Exercise 3 [10 points]: 

Define a FixdLenStringList class that encapsulate a list of strings and a string length. All string in a FixdLenStringList have the same length. There is not limit on number of strings (dynamic array) that a FixdLenStringList can hold. For example, 

The FixdLenStringList list1 may contain the strings aa, bb, cc, and dd (all strings of length 2). 

The FixdLenStringList list2 may contain the strings cat, dog, pig, fox, bat, and eel (all strings of length 3). 

An incomplete implementation of the FixdLenStringList.java class is provided with this exercise: 

Here are the required methods in the FixdLenStringList class that you need to implement. 

i) Public constructor – This constructor will have one parameter that will indicate the length of each string in FixdLenStringList’s list. The constructor will initialize strLength with this value and will initialize FixdLenStringList’s list of strings, possibleStrings, to have 0 entries. 

ii) You are to implement the found method. This method returns true if its String parameter key is found in the list of strings, false otherwise. 

iii) You are to implement the addString method. This method will add a string to the FixdLenStringList’s list if it is the correct length and not already in the list. If the string is already in the list or if the string is not the correct length, it is not added. 

HINT: you may call method found that is specified in next part of this problem. iv)  You are to implement the method removeRandomString that will remove and return a random string entry from FixdLenStringList’s list of strings. 

Write a client test class for testing the FixdLenStringList class. Supply, your client class along with screen shots of some test runs. 

Exercise 4: [20 Points] 

In this exercise, we will create a memory game called “Mind Game”. In this game, even number of tiles are placed face-down in rows on a “Mind Game Board”. Each tile contains an image. For each tile, there is exactly one other tile with the same image. The player is to pick a tile, see which image it has, and try to find its match from the remaining face-down tiles. If the player fails to turn over its match, both tiles are turned face-down and the player attempts to pick a matching pair again. The game is over when all tiles on the board are turned face-up. 

In this implementation of a simplified version of Mind Game, 

• Images will be strings chosen from a FixdLenStringList. (Use the FixdLenStringList class from exercise 3). 

• The mind game board will contain even number of Tiles. 

• The board will be implemented as a one-dimensional array of Tiles. 

For example, if the size of the mind game board is requested to be 4, then the board will have 16 tiles. The one dimension array, gameBoard, can be viewed as a 4-by-4 mind game board as follows: 

gameBoard[0] gameBoard[1] gameBoard[2] gameBoard[3] gameBoard[4] gameBoard[5] gameBoard[6] gameBoard[7] gameBoard[8] gameBoard[9] gameBoard[10] gameBoard[11] gameBoard[12] gameBoard[13] gameBoard[14] gameBoard[15] 

An incomplete implementation of the Tile and Board classes are provided with this exercise (Tile.java, Board.java). 

You should examine these classes and complete them with the following methods. To implement the methods, see the specifications provided in the comments of the methods. 

a) Write the implementation of the Board method fillBoard, which will randomly fill the mind game board with Tiles whose images (strings) are randomly chosen from the strings contained in the FixdLenStringList. A tile image that appears on the board appears exactly twice on the board. 

b) Write the implementation for the Board method lookAtTile. This method will call the appropriate method of the Tile class to turn the Tile face-up. 

c) Write the implementation for the Board’s method checkMatch. This method will check whether the tiles in its two integer parameter positions on gameBoard have the same image. If they do, the tiles will remain face-up. If they have different images, then tiles will be turned face-down. (See the comments that were given in the problem specification). 

d) Complete the implementation for printBoard so that the Mind Game board is printed as described in the comment. For example, you should call the Board method format to right-justify the printing of the Tile image or the printing of the Tile position. An example of printBoard for a 4 by 4 mind game board that is partially solved is shown below after 4 matches have been found. The FixdLenStringList passed as a parameter to Board constructor contains strings of length 3. 

fox fox dog dog cow 5 6 7 cow cat  10 11 12 13 14 15 

Write a MindGame.java class that will handle user input and simulate the mind game to the user. An example run of the mind Game is provided below: 

0 1 

2 3 

Choose positions:1 2 

pig , cat 

0 1 

2 3 

Choose positions:0 1 cat , pig 

0 1 

2 3 

Choose positions:0 3 cat , pig 

0 1 

2 3 

Choose positions:1 3 

pig , pig 

0 pig 

2 pig Choose positions:0 2 

cat , cat cat pig cat pig 

These exercises are based on GUI, and are extracted from https://www3.ntu.edu.sg/home/ehchua/programming/java/GraphicsExercises. html#zz-4.1 Exercise 5 [5 points]: 

SwingTemperatureConverter 

Write a GUI program called SwingTemperatureConverter to convert temperature values between Celsius and Fahrenheit. User can enter either the Celsius or the Fahrenheit value, in floating-point number. 

Hints: To display a floating-point number in a specific format (e.g., 1 decimal place), use the static method String.format(), which has the same form as printf(). For example, String.format(“%.1f”, 1.234) returns String “1.2”. 

Exercise 6 [5 points]: 

Write a number guessing game in Swing (as shown in the Figure). The program shall generate a random number between 1 to 100. It shall mask out the random number generated and output “Yot Got it”, “Try Higher” or “Try Lower” depending on the user’s input. 

Hints: 

• You can use Math.random() or rnd.nextInt(100) to generate a random number in double in the range of [0.0, 1.0). 

Exercise 7 [5 points]: 

Write a generic static method called “search” that will take two parameters, i) an array containing generic objects and ii) a reference to a generic object. The search method should look for the index of the passed object in the array. If the passed object is not found in the array, then the method should throw a java.util.NoSuchElementException (notice that this is a RunTimeException) exception. 

Hint: In the search method, you should use the Object.equals method for finding the object in the array. 

Now, create a main method in the same class and test the functionality of the search method by passing some “Point” objects and some “String” objects respectively. You should also handle the exception in the main method and show an appropriate message in case the object is not found in the array, otherwise print the index where the object is found in the array. 

Note: you may need to override the .equals method in the Point class for the search method to find a particular Point in the Point array. Here is a sample Point class, which you are free to modify. 

class Point { private int x, y; 

public Point(int x, int y) { this.x = x; this.y = y; 

public Point(){ this(0, 0); 

public int getX() { return x; 

public void setX(int x) { this.x = x; 

public int getY() { return y; 

public void setY(int y) { this.y = y; 

@Override public String toString() { 

return “Point{” + “x=” + x + “, y=” + y + ‘}’; 

Calculate the price of your order

You will get a personal manager and a discount.
We'll send you the first draft for approval by at
Total price:
$0.00
Pay Someone To Write Essay