java - How to create an object from data in a file and assign it to an array? -
I need to take information from a file and put them into objects and insert them into an array so that I compare these areas By doing so, the objects and lists in the array, which contain the largest area of the object and its position in the array.
I'm confused about how I get information from the file and every single object (circle or rectangle) and then that object is created after an array. I think my other classes are fine, I'm just stuck in the finish line.
Normally, I will do something like circle C1 = new circle (); How to create a new object, but how can I do with a file with the predefined notification and specify it in an array?
Data:
â ???? CIRCLE ????, 1, is a blue color, true, on one side, 1, 2, a blue color, is true ??? ? RECTANGLEâ ????, 10, 2, a red ????, is a "cycle" true, 2, a "green" one ???? RECTANGLEâ ???? One ???? CIRCLE driver:
import java.io.File; Import java.io.phileNotFoundException; Import java.util.ArrayList; Import java.util.Scanner; Public Static Wide Men (String [] AGRs) Flannett throws foreign exposure {Scanner input = new scanner (new file ("C: /Users / Charles / Desktop / Gymmetric object.dta.txt")); ArrayList & LT; GeometricObject & gt; List = New Arrestist & lt; Geometric Object & gt; (); While (input.hasNext ()) {string line = input Newlynline (); Println (line); }}} Geometric object:
public abstract class geometric object {// class variables private string color; Private bullion filled; // constructor public geographic object () {super (); Color = "white"; Filled = False; } Public geometrical object (string color, boolean) {super;); This.color = color; this. Fill = fill; } // mutator public string getColor () {color of return; } Public Zero Set Collar (string color) {this.color = color; } Public boolean is filled () {loaded return; } Public Zero Setfield (Boolean Filled) {this.filled = fill; } // user-defined methods public essence dual getArea (); Public Abstract Double GetPerimeter (); @ Override public string toasting () {Return Super. Tosting () + "\ tColor =" + this.getColor () + "\ tFilled =" + this.isFilled (); }} Circle:
Public square circle provides physical double radius {// class variable}; // Constructor Public Circle () {super (); Radius = 1; } Public Circle (double radius, string color, fill boolean) {super (color, filled); This.radius = Radius; } // mutator public double mill RDIS () {return radius; } Public Zero Set Radius (double radius) {this.radius = radius; } // User-Defined Methods @ Override Public Double GetArea () {Area of a Circle Return (Radius * Radius * Math.PI); } @ Override Public Double Hot Preliminary () {// Circular Return Perimeter (2 * Radius * Mathematics. PI); } @ Override public string toString () {return super.toString () + "\ n cycle: radius =" + this.getRadius (); }} Rectangle:
Public square rectangle geometric object {// class squared personal double height; Private double width; // Constructor Rectangle () {super}; Height = 1; Width = 1; } Public rectangle (double height, double width, string color, boolean fill) {super (color, filled); This.height = Height; This.width = width; } // mutual public double hoite () {height back; } Public Zero SatHight (double height) {this.height = height; } Public double getWidth () {return width; } Public Zero Set Width (double width) {this.width = width; } // user-defined methods @ override public string toasting () {return super. Tosting () + "\ nReference: Height =" + this.height + "\ tWidth =" + this.width; } @ Override Public Double GoAria () {Return (height * width); } @ Override Public Double Hot Primer () {Return (2 * height + 2 * width); }}
- In your text file, there are special quotes around your size Item is not there.
- Example of creating an object (by its main method):
This will make your life even more difficult, while (input.hasNext ()) {String line = input NXtine (); Println (line); String [] parts = line. Split (","); If (parts [0] .indexOf ("Circle")! = -1) {Circle C = new circle (); // ... ... to parse the remaining properties to set your spelling) and if ... // fill in other size cases}
Comments
Post a Comment