Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 132,133 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,050 people online right now. Registration is fast and FREE... Join Now!




How to pull attributes from another object

 
Reply to this topicStart new topic

How to pull attributes from another object

Jeep2000
post 10 Oct, 2008 - 08:14 AM
Post #1


New D.I.C Head

*
Joined: 10 Oct, 2008
Posts: 1

The beginning Java concepts are just starting to sink in with me, but I've been stuck on the same step of this exercise for several days now. I was hoping someone could point me in the right direction.

Description:
Create a class named TVProgram that has a field to store the name, the time and day of the program, the program information, cost, and whether it is a repeat or not.
That part is working as I can get the desired results.
The next step is to create a fourth program that uses the attributes from one of the three objects. This is what I cannot wrap my head around right now; how to reach in and get attributes from one of these objects.
Here is what I have so far:

CODE

package one;


/**
* Created by
* October 6, 2008
*
*
*/
public class Main {

     public static void main(String [] args){

// Declare programs
    TVprogram TVprogram1 = new TVprogram("Magnum PI", 21, "Monday",
"Private detective", true, 1000.0f);
    TVprogram TVprogram2 = new TVprogram("Lost", 19, "Wednesday",
"Plane crash survivors", false, 200000.0f);
    TVprogram TVprogram3 = new TVprogram("Today", 7, "Thursday",
"News", false, 10000.0f);
//TVprogram TVprogram4 = new TVprogram(???);

// Print the information using the toString method.
    System.out.println("Program 1 data: " + TVprogram1.toString());
    System.out.println();

    System.out.println("Program 2 data: " + TVprogram2.toString());
    System.out.println();

    System.out.println("Program 3 data: " + TVprogram3.toString());
    System.out.println();
    
   // System.out.println("Program 4 data: " + TVprogram4.toString());
   // System.out.println();

    System.out.println(TVprogram1.toString());
    System.out.println();
    System.out.println(TVprogram2.toString());
    System.out.println();
    System.out.println(TVprogram3.toString());
    System.out.println();
    }
}


CODE


package one;

/**
*
* Created by
* October 6, 2008
*
*/
public class TVprogram {

//the attributes of program
    private String name, pname;
    private int time;
    private String day, info;
    private boolean repeat;
    private float cost;
    
//constructor
    public TVprogram(String programName,
            int programTime,
            String programDay,
            String programInfo,
            boolean programRepeat,
            float programCost) {
                 name = programName;
                 time = programTime;
                 day = programDay;
                 info = programInfo;
                 repeat = programRepeat;
                 cost = programCost;
                 }

    public String getName() {
        return name;
    }

    public int getTime() {
        return time;
    }

    public String getDay() {
        return day;
    }

    public String getInfo() {
        return info;
    }

    public boolean getRepeat() {
        return repeat;
    }

    public float getCost() {
        return cost;
    }
    
    public String getPname() {
        return this.pname;
    }

// Creating set methods
    public void setProgramName(String givenName) {
        name = givenName;
    }

    public void setProgramInfo(String givenInfo) {
        info = givenInfo;
    }

// Creating toString method
    public String toString()    {
        return  name + "\t" + info
                + "\nWhen: " + day + "\t" + time
                + "\nRepeat: " + repeat
                + "\nCost: " + cost;
    }

}



Any help would be much appreciated. I have spent hours Googling and going through books and cannot figure out what concept I am missing.
User is offlineProfile CardPM

Go to the top of the page

JeroenFM
post 10 Oct, 2008 - 10:21 AM
Post #2


D.I.C Head

Group Icon
Joined: 30 Jun, 2008
Posts: 182



Thanked 9 times

Dream Kudos: 100
My Contributions


You're quite close actually:

java


TVprogram program = new TVprogram("Magnum PI", 21, "Monday",
"Private detective", true, 1000.0f);

String name = program.getName();



Just call the method that passes the private members of the class, the so-called "getters".
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/21/08 12:04PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month