Results 1 to 3 of 3
  1. #1
    merdzins is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default getter and setter method help please!

    Hi all! I am extremely new to java and to computer programming in general so I greatly appreciate all our help. Here's my problem, I am trying to write a program that reads in a basic text file with some names and some numbers, and inputs the given information into an array list. Each line of the text file is supposed to serve as a separate object in the array list. The major problem for me is that I am required to use 2 classes to do this. One class must be the main class and the other class is supposed to hold Get and Set methods for each piece of information in each object in the ArrayList, and I am very clueless as to how to use get and set methods. Here is the code that I have so far....i know the Driver class is where the get and set methods are supposed to go...i just dont know what they are supposed to look like or how to do them. Please help me if you can, I am very desperate! Thank you so much in advance.

    Here is the main code:

    Java Code:
    import java.util.ArrayList;
    import java.util.Scanner;
    import java.util.*;
    import java.io.*;
    import java.awt.*;
    
    public class C_Part {
    
    /********************************************************************
    Creates the C_Part Class
    ********************************************************************/
    public static void main (String[]args)
    {
    Scanner scan = new Scanner(new File(args[0]));
    Scanner scanner = null;
    ArrayList <Driver> drivers = new ArrayList <Driver> ();
    
    if (args.length != 1)
    {
    System.out.println ("The name of the file to be read for " +
    " the report is expected.");
    System.exit(1);
    }
    try
    {
    File file = new File (args[0]);
    scanner = new Scanner (file);
    }
    catch(IOException ioe)
    {
    System.out.println ("The file " + args[0] + " does not exist");
    System.exit(1);
    }
    
    int count = 0;
    
    
    while (scanner.hasNext()){
    scanner.nextLine();
    count++;
    } 
    for (int i=0; i<count; i++){
    
    
    
    String Fname = scanner.next();
    String Lname = scanner.next();
    double fares = scanner.nextDouble();
    double gaspaid = scanner.nextDouble();
    double distance = scanner.nextDouble();
    double gallons = scanner.nextDouble();
    
    Driver d = new Driver(Fname, Lname, fares,
    gaspaid, distance, gallons);
    
    drivers.add(d);
    }
    
    
    
    d.setfirstname(Fname);
    d.setlastname(Lname);
    }
    }
    And here is the Driver that is supposed to hold get and sets!

    Java Code:
    public class Driver
    {
    public String Fname;
    public String Lname;
    private double fares;
    private double gaspaid;
    private double distance;
    private double gallons;
    
    public Driver (){
    getFname(String firstname);
    
    setFname(String firstname);
    
    
    
    }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,573
    Rep Power
    12

    Default

    The Driver class seems to be representing a driver with a name and some other characteristics. There are two things that must be included in the Driver class.

    The first is a constructor which creates a new driver when it is passed all the necessary information. The second are the setter and getter methods you have been asked to write. A setter method just sets some piece of information (like the first name) based on the argument that it is sent. A getter does the opposite: it returns the appropriate information about a driver.

    Your Driver class will look like this:

    Java Code:
    public class Driver {
        private String firstName;
        private String lastName;
        private double fares;
        private double gasPaid;
        private double distance;
        private double gallons
    
            /** Creates a new driver based on the supplied arguments. */
        public Driver(String firstName, String lastName, double fares,
                double gasPaid, double distance, double gallons) {
            this.firstName = firstName;
            // etc
        }
    
            /**
             * Sets this driver's first name to the supplied value.
             */
        public setFirstName(String firstName) {
            // code here
        }
    
            /**
             * Returns this driver's first name.
             */
        public String getFirstName() {
            // code here
        }
    }

    Notice how the line "this.firstName = firstName" sets the first name of the driver being created to the firstName argument that was supplied. This is probably what you want the constructor to do. The setFirstName() setter method will do something similar.

    It might be a good idea to complete the Driver class (ie complete the constructor and add the other setter and getter methods) because it is relatively straightforward and then move on to other class.

  3. #3
    merdzins is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default Thank You!!

    Wow seriously you are a lifesaver!! I figured it all out with the help you gave me and I am seriously so thankful! It took me a lot of experimenting but I literally would not have solved this without your help! thanks again!!

Similar Threads

  1. use of private in getter and setter
    By katturv in forum New To Java
    Replies: 1
    Last Post: 12-03-2010, 08:17 PM
  2. private static setter method problem
    By kyussy in forum New To Java
    Replies: 7
    Last Post: 01-27-2010, 05:02 PM
  3. Null Pointer Exception via setter method?
    By zerkz in forum New To Java
    Replies: 9
    Last Post: 10-29-2009, 10:00 PM
  4. Replies: 4
    Last Post: 01-01-2009, 01:03 PM
  5. problem in getter method
    By freddieMaize in forum Advanced Java
    Replies: 5
    Last Post: 05-26-2008, 08:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •