Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Need help solving this class assignment.

  1. #1
    Junior Member
    Join Date
    Apr 2019
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Need help solving this class assignment.

    /*
    * Program: ArrayListExample (Snippet 8b)
    * Programmer: [Ray Holguin]
    * Date:[April 15,2019]
    * Description: This program will ask the user to enter the 3 dimensions of a
    * room, then store the room object in a list rooms. This program will also
    * store the data in an external file.Each time the program is started, it
    * will load the data from the external file.
    */
    package arraylistexample;

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.util.ArrayList; // Needed to use ArrayLists
    import javax.swing.JOptionPane; // Needed to use JOptionPane methods

    public class ArrayListExample {
    // Globel varaible
    static String fn = "Room_Data.txt"; // File that will store the data

    public static void main(String[] args){
    // initialization
    String depth, width, length; // The 3 dimisions of the room
    ArrayList<BoxClass> rooms = new ArrayList(); // A list of rooms (box)

    // Load the data into the Array List from the external file
    try {

    } catch (Exception e) {
    }

    // Loop until user press the "No" button
    do {
    // Ask the user for the depth of the room
    depth = JOptionPane.showInputDialog(null,
    "Please enter the depth of the office",
    "Room Database", JOptionPane.QUESTION_MESSAGE);

    // Ask the user for the width of the room
    width = JOptionPane.showInputDialog(null,
    "Please enter the width of the office",
    "Room Database", JOptionPane.QUESTION_MESSAGE);

    // Ask the user for the length of the room
    length = JOptionPane.showInputDialog(null,
    "Please enter the length of the office",
    "Room Database", JOptionPane.QUESTION_MESSAGE);

    // Use the Box Class constructor to build the room (Object)
    Box room = new Box(depth, length, width);

    // Add room to the arrayList
    boolean add;
    add = rooms.add(room);

    // Save the room's data to the external file
    writeData(room);

    // Ask the user if they want to enter a new room. The dialog box
    // will include a Yes/No Button - If Yes is clicked, loop
    } while(JOptionPane.showConfirmDialog(null,
    "You have " + rooms.size() + " rooms stored\n" +
    "Do you want to enter another room?", "Room Database",
    JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION);

    // Print the list of rooms
    showList(rooms);

    } // End of 'main'

    private static void showList(ArrayList<Box> list) {
    // Print the list of rooms using a collection loop. The ArrayList is
    // passed from the main method.
    int roomNum; // will hold the number for each room
    String output = "List of Rooms:\n"; // Output string of all rooms

    // For each box (i) in the ArrayList rooms.
    for (Box i : list) {
    // Take the index number of the current room (i), but since indexes
    // start with 0, you ave to add 1.
    roomNum = list.indexOf(i) + 1;

    // concatenate one line room dimensions per room
    output += "Room " + roomNum +
    "> D: "+ i.depth +
    " L: " + i.length +
    " W: " + i.width +"\n";
    }
    JOptionPane.showMessageDialog(null, output, "Room Database",
    JOptionPane.INFORMATION_MESSAGE);
    } // End of ShowList method

    private static ArrayList<Box> loadData(ArrayList)<Box> rooms) {
    // Read the data from the external file and load the ArrayList rooms
    String fileData = ""; // Will hold all of the data from the file
    String line; // Will hold one line of the file at a time
    String[] fArray; // Will hold the dimensions from one line of data

    // Create a trap if the I/O has an error
    try{
    // Open the file and read the first line
    BufferedReader br = new BufferedReader(new FileReader(fn))) {

    // Check to see that there is fdata on the current line in the file
    while ((line = br.readLine()) !=null) {
    // Load string into the array, comma delimiter for the data
    fArray = line.split(",");

    // Create a new room based on the dimensions that were store.
    // Since we know that the first number stored is the depth and
    // had the array index of 0, we are just use the index number
    // to reference the values in the array
    Box room = new Box(fArray[0], fArray[1], fArray[2]);

    // Add the room to the room list - rooms
    rooms.add(room);
    }
    // Once done with the file, destroy the thread to the file
    br.close();

    } catch (Exception e){
    // Send the exception to the output psane. 'e' will be the exception
    // created when an error occurs in the 'try' section.
    System.out.println(e);
    } // End of Try/Catch

    // Return the Room list back to the main method
    return rooms ;

    } // End of loadData method

    private static void writeData(Box room) {
    // Save the room data into an external file. Data from each room will
    // be stored on one line.

    // Create a trap if the I/O has an error
    try{

    // Convert the data into a String and add comma as a delimiter.
    // Append the data into the file ending with carage return.
    try ( // Create a FileWriter - Append = true
    FileWriter fw = new FileWriter(fn ) {
    // Convert the data into a String and add comma as a delimiter.
    // Append the data into the file ending with carage return.
    fw.append(Double.toString(room.depth) + ','+
    Double.toString(room.length) + ',' +
    Double.toString(room.width) + "\r\n");
    // Once done with the file, close the tread to the file
    }
    '

    }catch (Exception e){
    // Send the exception to the output pane. 'e' will be the exception
    // created when an error occurs in the 'try' section. Errpr message
    // can only be seen by the programmer, not the user.
    System.out.println(e);
    } // End of Try/Catch
    } // End of writeData method

    private static void showList(ArrayList<BoxClass> rooms) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    private static class Box {

    private String depth;
    private String length;
    private String width;

    public Box() {
    }

    private Box(String depth, String length, String width) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    }
    } // End of Class

    /*
    * Program: BoxClass (Snippet 7b)
    * Programmer: [Ray Holguin]
    * Date: [April 9,2019]
    * Description: This class is used to create a box object. Class include area,
    * volumn and one display methods. This class has two constructor, so now the
    * user can create objects by passing number or strings.
    */
    package arraylistexample;

    class BoxClass {
    // Initialization
    double depth; // Depth of the box
    double length; // Height of the box
    double width; // width of the box

    // Constructor - user provides the values to create the box object
    public BoxClass(double d, double l, double w){
    // Take the values that are passed from the user to set the local var.
    this.depth = d;
    this.length = l;
    this.width = w;
    } // End of contructor

    // Overloaded constructor
    public BoxClass (String d, String l, String w){
    // Convert the passed String to number and set the Box dimensions
    this.depth = Double.parseDouble(d);
    this.length = Double.parseDouble(l);
    this.width = Double.parseDouble(w);
    } // End of constructor

    // Calculation methods
    public double volumn(){
    // Multiply the 3 dimensions to calculate the volumn of the box
    return depth * length * width;
    } // End of method

    public double area(){
    // Multiply the length and width to calculate the floor area of the box
    return length * width;
    } // End of method

    // Display method
    public String dimensions(){
    // Return a string that contain the 3 dimensions and summary
    return "Depth = " + depth + "\n" +
    "Length = " + length + "\n" +
    "Width = " + width + "\n" +
    "Floor area = "+ this.area() + "\n" +
    "Volumn = " + this.volumn();
    } // End of Method
    } // End of Class



    For those who are reading this message.I'm a beginners at this java assignment.I'm struggling at understanding it.If anyones willing to solve the code and to explain how you solved it.Im all ears and eyes.I looked all over websites and it is hard to find someone who is willing to teach me how to solve this code.I made a ArrayListExample and a Boxclass.I get some errors in the code.Someone taught me at the college and they were too in hurry to explain.If you have the time i have the time to look this over and to learn it.
    Attached Files Attached Files
    Last edited by TheNwoLegend; April 27th, 2019 at 10:18 PM.

  2. #2
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Need help solving this class assignment.

    Please put your code in the code brackets so that we can see it better.
    "Tick, tack"

  3. #3
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Need help solving this class assignment.

    Please place code between code tags (see BBcodes below for more info). And you didn't explain what problems you were having and whether they were, compiler problems, exceptions being thrown, or logic problems.

    And do provide attachments or images. We won't look at them.

    Regards,
    Jim

  4. #4
    Junior Member
    Join Date
    Apr 2019
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help solving this class assignment.

    Sorry for that guys.I'm having a hard time posting pictures where it can get the whole assignment.I had to copy and paste it so the code can be seen.Not every website allows large files.I feel like people are lazy to copy and paste the code to Netbeans.It's that simple or is it a problem.I can pay for a tutor so everyone can be happy.geez

    --- Update ---

    I'm not asking to cheat on this assignment.I'm asking everyone to show me how where the errors were and why i can't fix them.I have a disability which causes me to not grasp info that is hard.It takes me months before it grasps.I have a ADD disabilty.NO worries i can find someone who wants the money.Sorry for those who miss out.Lazy

Similar Threads

  1. Class structure for solving a thread situation
    By harry7ster in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 9th, 2013, 08:43 AM
  2. Hello, I have a class assignment and I need help.
    By Wings in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 17th, 2013, 11:49 PM
  3. My Class assignment help
    By ben_S21 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 4th, 2012, 01:35 AM
  4. class assignment
    By shanem in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 2nd, 2012, 01:23 PM
  5. class assignment
    By shanem in forum Java Theory & Questions
    Replies: 1
    Last Post: October 1st, 2012, 04:09 PM

Tags for this Thread