Class Computer (a data class as follows): private instance variables manufacture
ID: 3705947 • Letter: C
Question
Class Computer (a data class as follows): private instance variables manufacturer: String model: String memory: int storage: int public methods: a constructor that can make a new Computer object and assign values to all of its instance variables. a toString() method that shows the status of all instance variables. no other methods are required. Class StoreComputers creates an ArrayList of Computers (about 6 will suffice). writes the ArrayList to a file. Class ReadComputers retrieves the ArrayList from the file. displays all Computers in the list.
Explanation / Answer
package javaapplication15;
import java.util.*;
import java.io.FileWriter;
class Computer {
String manufacturer;
String model;
int memory;
int storage;
public Computer(String Manufacturer,String Model, int Memory, int Storage)
{
manufacturer=Manufacturer;
model=Model;
memory=Memory;
storage=Storage;
}
public String toString(){//overriding the toString() method
return manufacturer+" "+model+" "+memory+" "+ storage;
}
}
public class comp {
public static void main(String[] args)
{
String m,mo;
int mem,s,i=0;
////FileWriter writer = new FileWriter("output.txt");
Scanner sc=new Scanner(System.in);
ArrayList<Computer> list=new ArrayList<Computer>();
while(i<6)
{
System.out.print("Enter the Name of Manufacurer: ");
m=sc.next();
System.out.print("Enter the Name of Model: ");
mo=sc.next();
System.out.print("Enter the Memory: ");
mem=sc.nextInt();
System.out.print("Enter Storage: ");
s=sc.nextInt();
Computer cmp=new Computer(m,mo,mem,s);
System.out.print(" Details: "+cmp.toString());
list.add(cmp);
//writer.write(cmp.manufacturer" "cmp.model);
i++;
}
//writer.close();
}
}