aspose file tools*
The moose likes Beginning Java and the fly likes don't know why i need to use get methods while constructor does it Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "don Watch "don New topic
Author

don't know why i need to use get methods while constructor does it

Garry Meax
Ranch Hand

Joined: Feb 11, 2005
Posts: 31
/*
Create a class named Vehicle that includes,model(String) and
registration number(String).

Include get methods for these variables, and a constructor that
requires an arguments (model,registration number).

Create an extended class named Car whose constructor requires a owner's name.

Write a program to demonstrate creating and using an object of each class.
*/


import java.io.*;

class Vehicle
{
String model;
String regno;

Vehicle(String m, String r_no)
{
model = m;
regno = r_no;
}

void get_data()
{
// don't know why he is asking for this when i've constructor
}

void show_vehicle_data()
{
System.out.println("Model: "+model);
}
}

class Car extends Vehicle
{
String owner_name;

Car(String m, String r_no, String o_n)
{
super(m, r_no);
owner_name = o_n;
}

void get_data()
{
// don't know why he is asking for this when i've constructor
}

void show_car_data()
{
System.out.println("Registration No: "+regno);
System.out.println("Owner's Name: "+owner_name);
}

public static void main(String args[]) throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String model;
String regi_num;
String own_name;

System.out.println("Enter Model: ");
model = in.readLine();

System.out.println("Enter Registration No: ");
regi_num = in.readLine();

System.out.println("Enter Owner's Name: ");
own_name = in.readLine();

Car C = new Car(model, regi_num, own_name);

C.show_vehicle_data();
C.show_car_data();
}
}

my question is, i don't whats the purpose of using get_data methods while constructor is doing that for me.
Igor Stojanovic
Ranch Hand

Joined: Feb 18, 2005
Posts: 58
Your is doing job for get_methods... just put your code from those 2 methods inside get_methods and delete show_vehicle_data, show_car_data



kind regards
Igor
Garry Meax
Ranch Hand

Joined: Feb 11, 2005
Posts: 31
Originally posted by Igor Stojanovic:
Your is doing job for get_methods... just put your code from those 2 methods inside get_methods and delete show_vehicle_data, show_car_data

kind regards
Igor


thanks for the reply

my assignment requires me to use get_method to get data and at teh same time asking me to use constructors.

as you can see i have done it without using get_methods(but thats not what my teachers wants).

as i ma using constructor to pass the data, i don't know how to use get_methods and constructor at the same time.
Liam Tiarnach
Ranch Hand

Joined: Aug 06, 2004
Posts: 51
Originally posted by Garry Meax:


my assignment requires me to use get_method to get data and at teh same time asking me to use constructors.

as you can see i have done it without using get_methods(but thats not what my teachers wants).

as i ma using constructor to pass the data, i don't know how to use get_methods and constructor at the same time.


Wow, must of fell a sleep in class on that one...

okay, let's give it the quick break down...

constructor - used in constructing your class by doing things like initializing variables to default values, instantiating other classes that may be need by your class and whatever else it need...

get method - a method to that allows you to retrieve data from your class like the car name or whatever...

set method - a method that will set or change a value for a variable in your class...

example...


so you contructor is not the same as your get method, they are totally different syntax and have different purposes...


- Liam...<br />- ' He who never sleeps... '
Garry Meax
Ranch Hand

Joined: Feb 11, 2005
Posts: 31
Originally posted by Liam Tiarnach:

get method - a method to that allows you to retrieve data from your class like the car name or whatever...


is this method is used only to retrieve data from my class. i thought i was supposed to use this method to get input from the user.
Igor Stojanovic
Ranch Hand

Joined: Feb 18, 2005
Posts: 58
Usually when you see "getSomething" method it means that method is used to get some data from your class (so you can use it somewhere in your program) and "setSomething" method is used to get data from user and store it in your class...



kind regards
Igor
Garry Meax
Ranch Hand

Joined: Feb 11, 2005
Posts: 31
import java.io.*;

class Vehicle
{
String model;
String regno;

Vehicle(String m, String r_no)
{
model = m;
regno = r_no;
}

String get_model()
{
return model;
}

String get_regno()
{
return regno;
}
}

class Car extends Vehicle
{
String owner_name;

Car(String m, String r_no, String o_n)
{
super(m, r_no);
owner_name = o_n;
}

String get_owner_name()
{
return owner_name;
}

void show_car_data()
{
System.out.println("Registration No: "+regno);
System.out.println("Owner's Name: "+owner_name);
}

public static void main(String args[]) throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String model;
String regi_num;
String own_name;

System.out.println("Enter Model: ");
model = in.readLine();

System.out.println("Enter Registration No: ");
regi_num = in.readLine();

System.out.println("Enter Owner's Name: ");
own_name = in.readLine();

Car C = new Car(model, regi_num, own_name);

System.out.println("Model: "+C.get_model());
System.out.println("Model: "+C.get_regno());
System.out.println("Model: "+C.get_owner_name());
}
}

well liam, am i using the get_method correctly.
Liam Tiarnach
Ranch Hand

Joined: Aug 06, 2004
Posts: 51
Originally posted by Garry Meax:

well liam, am i using the get_method correctly.


Yup... looks good to me...
Garry Meax
Ranch Hand

Joined: Feb 11, 2005
Posts: 31
thanks,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: don't know why i need to use get methods while constructor does it