473,729 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Java Getter and Setter Question!!!

51 New Member
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
5 2513
Ganon11
3,652 Recognized Expert Specialist
What part are you having difficulty with?
Nov 14 '07 #2
javatech007
51 New Member
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
3,652 Recognized Expert Specialist
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
javatech007
51 New Member
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
3,652 Recognized Expert Specialist
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

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

Similar topics

0
1256
by: Mike Whitman | last post by:
I have a project in which I need to export articles from a java publishing system to RSS XML files. What java tool(s) should I look at? I would think there would be java tool that I could give a RSS DTD to and then map xml fields to data. Suggestions?
1
2572
by: Raquel | last post by:
Have a question on the Stored procedure method code generated by DB2 development center for Java stored procedures. Suppose I have a requirement to return the resultset consisting of FIRSTNME, LASTNAME of all employees belonging to a particular department, following is the stored procedure method code generated by "DB2 development center": public static void sproc3mtd ( String workdept, String firstnme,
30
10942
by: Richard | last post by:
Level: Java newbie, C experienced Platform: Linux and Win32, Intel Another programmer and I are working on a small project together. He's writing a server process in Java that accepts input from processes I've written over a TCP connection. My processes are all written in C; his are all done in Java. He's new to Java, and I've never really used it. My input is basically a stream of 32-bit unsigned integers (e.g., the
1
1922
by: Steve | last post by:
I generate C# webservices proxy code from WSDL file, it turns out the classes generated have public member variables and no getter/setter methods as follows, and I am able to get data when running the client. public class MyFeeResponse {
3
3672
by: Sam | last post by:
I m trying to port a a Java app to C# and have two questions. i) In Java there is a java.util.Logging package that can be used to log at different levels. Is there an equivalent in C# ? 2) There is a Runtime.getRuntime.availableProcessors() method that returns the number of available processors to the process. How do I do that in C# ? I think I have to use a Win32 unmanaged API call and can do that - but need some pointers on where I...
0
943
by: Sam | last post by:
I m trying to port a a Java app to C# and have two questions. i) In Java there is a java.util.Logging package that can be used to log at different levels. Is there an equivalent in C# ? 2) There is a Runtime.getRuntime.availableProcessors() method that returns the number of available processors to the process. How do I do that in C# ? I think I have to use a Win32 unmanaged API call and can do that - but need some pointers on where I...
3
3914
by: MBReikowsky | last post by:
Ok this is homework, and I have wrestled with this for quite awhile now. My program is supposed to give an error message if the user enters any invalid entries. The program works great when the user enters an invalid integer, but my problem is trying to get it to give an error message when the user enters an invalid entry when asked if they want to continue. Currently right now it just runs again if an invalid entry is entered. But I need it to...
0
1260
by: su.k.mishra | last post by:
Hello Friends, Please Visit http://techinterviewquestion.blogspot.com site In this site you can find .Net,C#,OOPS,Java,JSP Interview Question With Answer. Cmpany asked question is also available.. Please Review
0
9284
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9202
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9148
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8151
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4528
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4796
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2165
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.