By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,725 Members | 1,048 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,725 IT Pros & Developers. It's quick & easy.

Java Getter and Setter Question!!!

P: 51
Hi,
I've been at this question all day and still just don't know what to do. It's the last of all the questions I have to do and can't figure it out!!

If anyone could help or give guidance it would be greatly appreciated!!

Question:
Write a class called shapes that accepts two variables into its constructor called a and b. Along with the getter and setter methods required, write a method called toPrinter() to output the coordinates of the shape in the following format: [a, b].
Next write a class called orange that is a child class of shapes. This time the constructor accepts in a, b (centre of the orange), and a radius. Override the toPrinter() method in the orange class to include outputting the radius, as well as a and b. Write a method to get the area of the orange. Include any relevant classes to run and test the application.

Many Thanks
Nov 14 '07 #1
Share this Question
Share on Google+
5 Replies


Ganon11
Expert 2.5K+
P: 3,652
What part are you having difficulty with?
Nov 14 '07 #2

P: 51
i have a good understanding of the getter and setter method but dont know how to start off the class with the constructors that accepts the two variables!!
Nov 14 '07 #3

Ganon11
Expert 2.5K+
P: 3,652
Let's start slow. Do you know how to write a constructor? Do you know how to write a function that accepts multiple arguments?
Nov 14 '07 #4

P: 51
Let's start slow. Do you know how to write a constructor? Do you know how to write a function that accepts multiple arguments?

Not really.
For the constructor would you put

Class Shape
{
public...etc
{
shape_1;
1= new shape(a,b);
etc.

class shape
{
int a;
int b;

.
.
.
Am i going in any way the right direction?
Aswell I wouldnt be able to write a function that accepts multiple arguments.
Nov 14 '07 #5

Ganon11
Expert 2.5K+
P: 3,652
OK, let's focus on a function first.

Let's say I wanted to write a function that would sum two numbers. Easy enough. Following the basic steps, I get this function:

Expand|Select|Wrap|Line Numbers
  1. public int getSum() {
  2.    return 1 + 2;
  3. }
That's kind of lame. It always returns 3. If I want it to return some other sum, I'd have to change the code itself. Instead, I'll make it use variables that are passed as arguments:

Expand|Select|Wrap|Line Numbers
  1. public int getSum(int first, int second) {
  2.    return first + second;
  3. }
Now I want to use this in my main function. So I'll do this:

Expand|Select|Wrap|Line Numbers
  1. public void static main(String[] args) {
  2.    int myVar = getSum(10, 20);
  3.    int a = 5, b = 15;
  4.    int mySum = getSum(a, b);
  5. }
The first time, I call it using two hard-coded numbers. The second time, I call it with two int variables I made. So that's you you can write a multi-argument function.

A Constructor for a class is like a starter method that gets everything ready so that the object can be used. For instance, if you make a new Car, there are a few things that need to be done before it can be ready to be driven.

1) It needs gas.
2) It needs wheels.
3) It needs a model number and name.
4) It needs an owner.

A constructor sets all these things up. Now, if this were code, the constructor might look like this:

Expand|Select|Wrap|Line Numbers
  1. public Car() { // Note there is no return type here.  Constructors never have a return type, and their name is always the same as a class.
  2.    gas = 20; // 20 gallons of gas.
  3.    frontLeftWheel = new Wheel();
  4.    frontRightWheel = new Wheel();
  5.    backLeftWheel = new Wheel();
  6.    backRightWheel = new Wheel();
  7.    modelName = "Ford Taurus";
  8.    modelNumber = 12345;
  9.    owner = new Person();
  10. }
This constructor sets all it's variables to starting values so that the Car can be used. But it always sets it to the same things. What if we want different cars each time? To do that, we make the constructor accept arguments, just like any other function:

Expand|Select|Wrap|Line Numbers
  1. public Car(int startingGas, String name, int num, Person driver) { 
  2.    gas = startingGas;
  3.    frontLeftWheel = new Wheel();
  4.    frontRightWheel = new Wheel();
  5.    backLeftWheel = new Wheel();
  6.    backRightWheel = new Wheel();
  7.    modelName = name;
  8.    modelNumber = num;
  9.    owner = driver;
  10. }
Note how we used the arguments to set up the car. So now, if I want to create a Dodge Caravan, model number 31416 and 12 gallons of gas, I'd write:

Expand|Select|Wrap|Line Numbers
  1. Person Ganon11 = new Person();
  2. Car ganonsCar = new Car(12, "Dodge Caravan", 31416, Ganon11);
and now ganonsCar has all the attributes I wanted it to have.

With this as a guide, can you write your constructor and methods?
Nov 15 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.