Part 1: NumbersInfo.java
Recall the numbers_info.cpp program from Programming Assignment 3.
In Eclipse, create a new package called assign8. In that package, create a new Java class called NumbersInfo. Add a main method to the class. To the main method, add Java statements for opening the numbers.txt file, reading in the numbers contained in the file, and outputting the five stats described in the Programming Assignment 3 specifications. (You may add other, helper methods to the NumbersInfo class, as needed. Be sure to use the public and static keywords for such methods, as in the header for main.)
In writing your Java program, you should follow the Programming Assignment 3 specs with one exception — your program output must match the output of the example exactly. The course staff will use a program to grade your program, so make certain you do not have any extra spaces or misspelled words, and that your output is in the same order as the example. (You should use the default precision for printing double values in Java.)
Part 2: Fraction.java
Recall the Fraction class from Programming Assignment 7: Part 1.
In the assign8 package you started in Part 1 above, create a new Java class called Fraction. Add data members, member methods, and a main method to this class in order to solve the same problem as in Programming Assignment 7. Consider the following notes on how to convert your C++ code to Java:
-
In Java, use the long type for the numerator and denominator data members.
-
Put the keyword public in front of every member function. Put the keyword private in front of every data member declaration. Do not use the keyword static anywhere, except for the header of main.
-
In Java, functions are defined where they are declared. Place the code for each function immediately beneath its declaration. (There is no header file or forward declarations.)
-
In Java, class declarations do not have a semicolon at the end.
-
Change the to_string and to_double functions so that they have these exact headers in the Java version:
-
public String toString()
-
public double toDouble()
-
Most of your Java methods will be very similar to the C++ versions. The biggest difference will be in the toString function — it will be much simpler.
-
In Java, declaring a Fraction variable does not create a Fraction object. You always need to create the object, like this:
Fraction f = new Fraction(1,2);
This Java statement declares a Fraction variable, creates a Fraction object, calls the object’s constructor, and stores the resulting object in the variable.
-
Put the code from your main function of the fraction_demo.cpp into the main method of this Fraction class.
-
When testing the toString method in main, note that Java will automatically call the toString function whenever a String representation of a Fraction object is needed. (This simplifies printing out Fraction objects, just print the object and it will be automatically converted to text using your toString method.)
-
If you want to compare strings for equality, use the equals method in Java (not == as in C++). Assume you have two strings in variables s and t. You can see if they were equal like this:
if(s.equals(t))
The equals method is part of every string. It returns true if the explicit parameter has the same text as the text in that string object.
-
You do not need to compute π, as in Step 4 of Programming Assignment 7.
-
- NOTE: We will test your Fraction class by calling the member methods. Follow the instructions in Programming Assignment 7 and here, regarding what the method signatures should be. Ask questions as needed. Deviating from these specifications in any way could result in a Fraction class that does not run with our test program, earning no credit.
reference of programming assignment 3
-
Write a program that opens a text file named numbers.txt (from the current directory), and then prints to the terminal this useful information about the numbers contained in the file:
- the count of all numbers in the file
- the count of all negative numbers in the file
- the average value of all of the numbers in the file
- the largest number in the file
- the number in the file that is closest to 0
Be sure to use informative messages as you print each. The ordering is not important.
Here is sample output printed to the terminal for this numbers.txt[img src=”https://utah.instructure.com/images/preview.png” alt=”Preview the document”>
file:
You are strongly encouraged to use incremental development for this program. Start by writing a program that simply reads the file and prints out each number. When that compiles and runs correctly, then try counting the numbers instead. When that compiles and runs correctly, also count the negative numbers. And so on, keeping this pattern — take small steps, test, and then move on to the next step.
For your own testing, use Emacs to create a numbers.txt file and type numbers into it. Make sure it is in the same directory as your program. Manually edit the file to change and/or add to the data and conduct additional tests.
The course staff will test your program on our own numbers.txt file. The file will contain only a list of numbers — nothing else (no letters or other non-number symbols). It may contain hundreds, thousands, or millions of numbers. You may assume that it contains at least one number.
reference of prog 7 part 1
Complete the program started in Lab 8 by doing the following four tasks:
-
Finish the second Fraction constructor to ensure the denominator is not negative. Recall that if a Fraction object is negative, its numerator is negative. And, a Fraction object should not have a non-positive denominator.
-
Complete the Fraction class by adding subtract and divide member functions. These functions should be similar to the existing add and multiply functions. Add declarations, comments, and implementations for both of these functions to the appropriate files.
NOTE: We will test your Fraction class by calling your functions. Therefore, they must be named exactly as stated. Furthermore, the subtract and divide function must have exactly the same parameter as the add and multiply functions. Deviating from these specifications in any way could result in a Fraction class that does not run with our test program, earning no credit.
-
Add code to fraction_demo.cpp to thoroughly demonstrate and test all functions of the Fraction class, including the constructors. Create and use at least five Fraction objects in addition to those created in Lab 8 and call each function in least three different cases. Your “tests” should simply be sequences of statements that compute and display the results of using the Fraction objects and functions. The user should be able to examine your code and descriptive output to see that the Fraction class is working properly.
-
Write a second program in a new file called pi.cpp to compute π using Fraction objects and the following summation:
Your new program should be separate from fraction_demo.cpp. To compile, use g++ with pi.cpp and fraction.cpp, but not fraction_demo.cpp.
Compute the summation above using Fraction objects. The summation is infinite, but you only need to sum up the terms for k = [0…3]. Use Fraction objects for the fraction terms shown above. When computing the summation, you can also use int or long long variables and values, but do not use any floating-point variables or values.
After you compute the summation, display the computed value of π as a fraction and as a floating-point number.
HINT: You can create a fraction like this:
-
Fraction f(4, 8*k+1);
Create the fractions, and then add, subtract, or multiply them as needed. Keep a running total (also as a Fraction object). Finally, use the conversion functions to display the results, as required above.
Submit all four files needed for both programs here. (Submission of the files for Part 2 are at a different location. More information on that is below.)


0 comments