Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

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

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
/*
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.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
Garry Meax
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Garry Meax:

well liam, am i using the get_method correctly.



Yup... looks good to me...
 
Garry Meax
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks,
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic