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 3 of 3

Thread: How do get and set methods work??

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do get and set methods work??

    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 maybe just an example or anything you can do to help I just need some help I've tried everything I know, I am very desperate! Thank you so much in advance.

    Here is the main 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!

    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
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How do get and set methods work??

    See Defining Methods for tips on how to write methods. 'Getters' and 'Setters' follow the same type, but set and get the values of the private variables. To use your code as an example:
    public void setGasPaid(double d){
        this.gaspaid = d;
    }
    public double getGasPaid(){
        return gaspaid;
    }

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do get and set methods work??

    Thanks for the help page link..

Similar Threads

  1. [SOLVED] Can't get JFreeChart to work
    By igniteflow in forum Java SE APIs
    Replies: 2
    Last Post: February 15th, 2011, 02:19 AM
  2. home work ..
    By shane1987 in forum Collections and Generics
    Replies: 8
    Last Post: November 16th, 2010, 09:45 AM
  3. My lines won't work!!!
    By The Mewzytion in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 2nd, 2010, 10:24 AM
  4. my run program does not work
    By rman27bn in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 16th, 2009, 09:13 AM
  5. Why won't this while loop work?
    By trueblue in forum Loops & Control Statements
    Replies: 2
    Last Post: July 17th, 2009, 09:10 AM