Input is from a file The first upper case character on each
Input is from a file:
The first upper case character on each line determines what type of object it is (C for Circle, R for Rectangle, F for Fraction, I for FeetInches).
A C will be followed by a Double (the radius of the Circle you will create)
An R will be followed by 2 Doubles, the length and width of the Rectangle you will create
An F will be followed by 2 ints, the numerator and denominator
An I will be followed by 2 ints, the feet and inches.
After you create each object put it into an ArrayList. (There will be 4 ArrayLists, one for Circles, one for Rectangles, one for Fractions, one for FeetInches) USE A SWITCH STATEMENT to determine which constructor to call (which object to make).
After you have input all your objects into the ArrayLists print them all out, First all the Circles from that ArrayList, then all the Rectangles, etc. State clearly what you are printing.
e.g. These are the Circles:you should make your own input file, call it H6.in but I shall use my own for grading so you will not know how many iyems are in the input or what order they are in.
Sample input file H6.in
H6 in History Source 1 C 4.5 2 F 4 8 3 R 3.6 7.8 4 I 4 50 5 F 1 4. 6 I 46 7 C 8.8Solution
//Save as Circle.java
public class Circle {
private double radius;
public Circle(double r) {
radius=r;
}
public String toString() {
return \"Circle with radius,r\"+radius;
}
}
--------------------------------
//Save as Rectangle.java
public class Rectangle {
private double length;
private double width;
public Rectangle(double length,double widht) {
this.length=length;
this.width=widht;
}
@Override
public String toString() {
return \"Rectangle with length,\"+length+\" and width ,\"+width;
}
}
--------------------------------
//Save as Fraction.java
public class Fraction {
private int num;
private int denom;
public Fraction(int num, int denom) {
this.num=num;
this.denom=denom;
}
@Override
public String toString() {
return \"Fraction = \"+num+\"/\"+denom;
}
}
--------------------------------
//Save as Inches.java
public class Inches {
private int feet;
private int inches;
public Inches(int feet,int inches) {
this.feet=feet;
this.inches=inches;
}
@Override
public String toString() {
return \"I =\"+feet+\" , \"+inches;
}
}
--------------------------------
/**
* The java program ReadShapes that reads a text file
* H6.txt and then creats objects of Circles, rectangles,
* fraction and inches and print each object to console.
* */
//ReadShapes.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class ReadShapes {
public static void main(String[] args) {
Scanner filereader=null;
String fileName=\"H6.txt\";
char ch;
//create arraylist of objects of type cirlce,rectangle,fraction
//and inches
ArrayList<Circle>circleList=new ArrayList<Circle>();
ArrayList<Rectangle>rectList=new ArrayList<Rectangle>();
ArrayList<Fraction>fractionList=new ArrayList<Fraction>();
ArrayList<Inches>inchesList=new ArrayList<Inches>();
try {
filereader=new Scanner(new File(fileName));
while(filereader.hasNextLine())
{
String line=filereader.nextLine();
String data[]=line.split(\" \");
ch=data[0].charAt(0);
switch(ch)
{
case \'C\':
double r=Double.parseDouble(data[1]);
circleList.add(new Circle(r));
break;
case \'R\':
double length=Double.parseDouble(data[1]);
double width=Double.parseDouble(data[2]);
rectList.add(new Rectangle(length, width));
break;
case \'F\':
int num=Integer.parseInt(data[1]);
int denom=Integer.parseInt(data[2]);
fractionList.add(new Fraction(num, denom));
break;
case \'I\':
int feet=Integer.parseInt(data[1]);
int inches=Integer.parseInt(data[2]);
inchesList.add(new Inches(feet, inches));
break;
}
}
//print circle objects
System.out.println(\"Circles\");
for (Circle circle : circleList) {
System.out.println(circle);
}
System.out.println();
System.out.println(\"Rectangles\");
//print rectangles objects
for (Rectangle r : rectList) {
System.out.println(r);
}
System.out.println();
System.out.println(\"Fraction\");
//print fraction objects
for (Fraction f : fractionList) {
System.out.println(f);
}
System.out.println();
System.out.println(\"Inches\");
//print Inches objects
for (Inches inche : inchesList) {
System.out.println(inche);
}
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
}
--------------------------------
H6.txt
C 4.5
F 4 8
R 3.6 7.8
I 4 50
F 1 4
I 4 6
C 8.8
--------------------------------
Sample output:
Circles
Circle with radius,r4.5
Circle with radius,r8.8
Rectangles
Rectangle with length,3.6 and width ,7.8
Fraction
Fraction = 4/8
Fraction = 1/4
Inches
I =4 , 50
I =4 , 6



