Need help with the Driver class. I have written 3 other classes that are shape c
ID: 3762953 • Letter: N
Question
Need help with the Driver class. I have written 3 other classes that are shape classes.. Circle, Rectangle and Triangle.
This is what I have so far on the Driver class..
import java.util.*;
import java.io.*;
public class Assignment7 {
public static void main(String theArgs[]) {
Scanner inputFile = null;
PrintStream outputFile = null;
try {
inputFile = new Scanner(new File("in7.txt"));
outputFile = new PrintStream(new File("out7.txt"));
} catch (Exception e) {
System.out.println("Difficulties opening the file! " + e);
System.exit(1);
}
Assignment7.java
This is the test driver class that will include main. This program MUST read a file named in7.txt and generate
an output file named out7.txt. The in7.txt file must be created by you based on formatting described shortly.
in7.txt and out7.txt are NOT to be included in the zipped file.
The input file “in7.txt” will contain multiple lines of input. The input is mostly valid input but, there may be some
invalid lines interspersed throughout the file. Valid input is as follows:
Single value: input for the radius of a circle
Two values separated by a space: input for the two sides of a rectangle, 1st = length, 2nd = width
Three values separated by a space: input for the three sides of a triangle in SideA, SideB, SideC order.
Lines containing anything else (could be anything above plus other data) or nothing at all are considered
invalid and should simply be ignored by your program (program continues to the next line). However, a line
such as: 3.2 -5.1 should throw an IllegalArgumentException within the Rectangle class due to the negative
value.
Again, valid lines will only contain 1, 2, or 3 values, for circles, rectangles, or triangles respectively. Lines with
“anything” else are invalid.
Decompose main by calling methods that input data into the List and output the List. Be sure to use a
try/catch block inside the input of data method. The try section should call an appropriate class to instantiate
an object (Circle, Rectangle, Triangle) passing the appropriate data for the radius, or sides, respectively. If the
constructor of the class discovers inappropriate data, it should throw a new IllegalArgumentException
(described above in each shape class). The exception, in turn, will be caught in the catch section of the
try/catch block and will print to the console an appropriate error message when an exception is thrown. The
program should not terminate but instead, continue to the next line of input.
Of course, if no exception occurs, execution will simply bypass the catch block and continue as it should for
valid input data.
As you input the data, you should create objects of the appropriate type (mentioned in the above paragraph)
and then insert them into a List of Shape objects using a LinkedList.
Your method for input should receive an already instantiated Scanner to the input file and the already
instantiated LinkedList of Shape (this will be filled inside the method and return, via the parameter, to main).
This method should also return a List of Shape (as an ArrayList) which contains all the values of the LinkedList
described above (this should be used as the List to be sorted).
The List passed to the input method should be declared as:
List<Shape> myList = new LinkedList<Shape>( ); // instantiate before sending it to the input method.
All calls to the input method should be (name the method anything you want):
List<Shape> copyList = getOriginalList(input, myList); // Remember the returned list is an ArrayList.
Sort the copyList and print out the myList, copyList, and then myList again by calling your output method 3
times.
To further demonstrate the power and flexibility of inheritance, polymorphism, and abstract classes, instantiate
your “copy” List as an ArrayList, have all methods that receive a List declare the parameter as List<Shape>.
Because your LinkedList and an ArrayList both implement List, either type can be sent to your output method
and used without changing any code.
After your Linked List has been filled, you can instantiate a new list and return it to the copyList as such:
List<Shape> newList = new ArrayList<Shape>();
for (Shape element : myList) {
Shape s = element.copyShape();
newList.add(s);
}
return newList;
Aside from the input/List creation process and testing the functionality of all these objects, your program
should:
Return a copied ArrayList of the original input LinkedList to a new list in main named copyList
Output the original list to display all the shapes and their area (pass it to the output method)
Sort copyList in ascending order by the shapes’ area (using Collections Class static sort method)
Display copyList in the sorted order (pass it to the output method)
Output the original list to display the original order (pass it to the output method)
All valid output (the above mentioned) should be sent to the output file out7.txt.
Exceptions thrown should report their output to the console.
Thanks!
Explanation / Answer
class OverloadDemo { void area(float x) { System.out.println("the area of the square is "+Math.pow(x, 2)+" sq units"); } void area(float x, float y) { System.out.println("the area of the rectangle is "+x*y+" sq units"); } void area(double x) { double z = 3.14 * x * x; System.out.println("the area of the circle is "+z+" sq units"); } } class Overload { public static void main(String args[]) { OverloadDemo ob = new OverloadDemo(); ob.area(5); ob.area(11,12); ob.area(2.5); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.