wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes I need help - basic stuff (I think) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I need help - basic stuff (I think)" Watch "I need help - basic stuff (I think)" New topic
Author

I need help - basic stuff (I think)

Harold Jools
Greenhorn

Joined: Mar 16, 2005
Posts: 17
I need some help here. I have two assignments here, that I am supposed to finish all by myself, but I just can�t get my head around this�and when I ask the �teacher�, he just says that he won�t help me. I�m on my own. I don�t know what kind of school this so called teacher went to, but I think that I should be allowed to ask for help, so now I�m asking you guys instead. I�m sure you can tell me what to do here. I thank you for all possible assistance that I can get here, because I myself am merely a beginner and I don�t know much about anything when it comes to this sort of thing, so�any help would be much appreciated�thanks in advance.
/Yurij


5a
Write a class which will be called Triangle. The class will be used as a model for different objects (triangles).
Test the class Triangle in a program (main class).
a)- Create two objects of Triangle. The objects� base and height can get different values. Print the objects� attributes (base and height) and their area (print method).
b)- Change base and height for existing objects (set methods) and print the altered objects� attribute once more.
c)- Get base and height for an object (get methods) and print them.

The test run should look like this:

a:
Object 1:
The area of a triangle with the base 3 and the height 8 is: 12.0
a:
Object 2:
The area of a triangle with the base 4 and the height 9 is: 18.0
b:
Object 1 with altered base:
The area of a triangle with the base 5 and the height 8 is: 20.0
b:
Object 2 with altered height:
The area of a triangle with the base 4 and the height 5 is: 10.0
c:
The objects� attributes printed using �get methods�:
The area of object 1 with the base 5 and the height 8 is: 20.0

Press Enter to exit_


class Triangle{
: // instance variables
public Triangle(int base, int height){
//Write code here
}
:
: //set and get methods.
:
public void print(){
//Write code here
}
}


5b
A company needs a program for their salary system to their employees. The model is to be based on so called �hourly wage�. The program should be able to calculate the employee�s salary (based on hourly wage and amount of hours) and print name, amount of hours and calculated salary. The employees who work more than 40 hours get overtime for the hours that exceed 40 and are calculated like this:
(40*hourly wage)+(amount of hours � 40)*(1 � )*hourly wage
To avoid too much overtime, a warning message should be printed when overtime exceeds 30 hours during a two week period.
Here is a possible scenario that is expected of the salary system (the program):
A user puts in the employee�s name and hourly wage. Then the amount of working hours is put in and the salary is calculated and printed week by week. You finish by pressing 0 and then the total amount of overtime hours shall be printed.
Test run: Your program should contain a class �Employee� which has for instance variables, one constructor and at least three methods: (salaryCalculation (int hours), getName() and toString().
As seen above, the program reads name and hourly wage. Then the program should create an object of the class �Employee� for the person put in.



class Employee {
// Instance variables
private String name;
private int hourly_wage;
private int lastWeeksOvertime;
private int totalOvertime;
// Methods
A constructor
A method for calculating salary
A method that returns name
A method called �toString()� and which shall return the last
text in the test run. (Name: Peter Johnson hourly wag�)
}
class Program5b {
public static void main(String[] args) {
// declaration of variables
// declaration of an object e.g. Employee;

:
:

}
}
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
I think you will find that asking people here to do your homework will get you nowhere fast. From your post it appears that you have done little/no work to find a solution. It appears that you have a fairly straightforward problem to solve and some skeleton code to get you started.

If you have specific questions regarding a specific place you are stuck, and have done some work to find a solution you will generally find people here very willing to help you (please note we are all volunteers here). Posting a homework question and asking for blanket help is the fastest way to get no help.

Most of us work as programmers and don't appreciate having people make it through school by having others do their work. Then when we have to work with them they know very little.

As I said, if you show some effort and ask better questions help is usually easy to find. And you might want to look at How to ask questions the smart way.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I'm with Steven on helping. Let us know how far you have gotten and we'll see what you might try next. Right now we don't know if you're stuck on creating classes, doing the math or checking the results.

BTW: I like it that your teacher provides tests to go with the assignment. That means you'll have a Triangle class that can be used in any program that needs triangles, and a test class that just proves Triangle is working. Learning to write the tests first is a great way to learn programming. The test program will look something like:

Making tests run is great fun. Show us what you have so far!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Harold Jools
Greenhorn

Joined: Mar 16, 2005
Posts: 17
OK...I realize now that maybe I need to be more specific with my questions. So...what exactly should my instance variables be in this case? I understand that the code should be something like "area=(base*height)/2;", right?
What I don't understand is how I am supposed to do when creating the two objects in a).

I really am trying to understand what I'm supposed to do here, and maybe just a little help could let me do that...
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
ok, let's start with the skeleton code for Triangle


It looks like the use case will be:

which should print out 'The area of a triangle with the base 3 and the height 8 is: 12.0'

So we need to store the values of base and height and be able to produce the area.
Harold Jools
Greenhorn

Joined: Mar 16, 2005
Posts: 17
Ok...so...let me understand. Is this one of the two objects then?:
Can't I just put base=3 and height=8 somewhere? Or is that not a valid thing here? Also, where do I put the code that you just wrote here?
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
I did pass base=3 and height=8 somewhere. I passed it into the constructor. You could also add getter and setter methods.

As for the code I wrote it would either go into another class (maybe called TriangleTest) or, more likely, into the main method of the Triangle class.
Harold Jools
Greenhorn

Joined: Mar 16, 2005
Posts: 17
Should the getter and setter methods also be placed in the constructor?
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
At this point it's time for you to write some code, try a couple things, maybe read a book for awhile. When you have some code written come back and post it and we can go from there.
amit smal
Greenhorn

Joined: Mar 17, 2005
Posts: 10
hey harold,
it will be useful if u do some coding yourself as u will get to know more concepts. knowing the concepts and implementing them is very important and u can learn this implementation only though practice. nevermind if u make mistakes. but give a try first
Ben Wood
Ranch Hand

Joined: Aug 14, 2001
Posts: 342
Harold,

I can sympathise with your predicament. I remember being thoroughly confused starting out with Java. I presume you've had lectures from your tutor, and hopefully have some notes? If so, I think the first step is to look back to any discussion of variables in classes. You need variables to hold the values of the base and height right?...your tutor's template actually suggests where to put these.



What you need to do is use the values passed intot the constructor to populate these two variables. Then you can think about doing the calculation.

HTH!


SCJP 1.4, www.gsi3d.org.uk
Harold Jools
Greenhorn

Joined: Mar 16, 2005
Posts: 17
Actually, I've had no lectures whatsoever, because this is an Internet based course, and we're supposed to learn everything by just reading books. However, when I'm reading books I have great difficulties in understanding how to use what I'm reading when I start writing code and that. It's all so confusing to me. I feel that I need to see something being done before I can do it myself. I don't know really...

But anyway...the instance variables would be "base=3" and "height=8" for example, right? Am I supposed to have four of these then, as I am supposed to create two objects, or?
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Don't worry about two object for now. Just worry about what you will need for step one:
a:
Object 1:
The area of a triangle with the base 3 and the height 8 is: 12.0

If we just start there and get that finished we can add to get the rest. So we will need a base and height variable. Then we will have to use these to get the area in the print method.
Harold Jools
Greenhorn

Joined: Mar 16, 2005
Posts: 17
Alright, I've done this now:


I'm not sure whether I've put all the stuff in the right places or not...I guess that's what's bothering me the most. I don't know which thing is which and where they should be.
Ben Wood
Ranch Hand

Joined: Aug 14, 2001
Posts: 342
Getting there! The concept you are missing is that if you define some variables at the class level (i.e. outside the constructor method) they become properties of the Triangle. So, when you create a new Triangle object by calling its constructor, the values you pass to the constructor are used to populate the variables for that object instance.



so, here, the values b and h are given to the constructor when you write code like this to create some different Triangle objects...



now you can call..



in a similar way you can also use the dot notation to say



You need to understand before you go much further with Java that classes are NOT objects, they are the blueprints for creating objects at runtime. So, when you define variables inside a class you get brand new copies of these variables each time you create a new Triangle using the new keyword. Each Triangle object has its own base, height and area properties.

HTH!
[ March 17, 2005: Message edited by: Ben Wood ]
Peter Rooke
Ranch Hand

Joined: Oct 21, 2004
Posts: 800

However, when I'm reading books I have great difficulties in understanding how to use what I'm reading when I start writing code and that. It's all so confusing to me.

Thinking In Java [Bruce Eckel]


Regards Pete
Ben Wood
Ranch Hand

Joined: Aug 14, 2001
Posts: 342
By the way, the next problem would be if "(base*height)/2" was nto a whole number, you will need to defin the area variable as a decimal like float or double type.
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
I'm going to add a bit to what Ben did.


That should be enough code for you to get the Triangle class working. There is still a bug in there as Ben mentioned.
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
Also you do not need to store the area, as you can easily create a method to give you the area.
Harold Jools
Greenhorn

Joined: Mar 16, 2005
Posts: 17
Alright, I will try all this later today or tomorrow.
I really appreciate the help fellas.
Thank you very much.
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
Harold, have you succesfully written, compiled and run anything yet? Even a simple "Hello World" app. If so great, carry on. If not I would suggest you do so. It sounds like you're coming across some things you aren't familiar with and it will be even more confusing if you're trying to get classpath set and the compiler to work for you while learing these new concepts(objects, variable etc...).
Harold Jools
Greenhorn

Joined: Mar 16, 2005
Posts: 17
I have compiled and run quite a few things actually, but I've never come across an assignment like this before, where I actually have to understand all these concepts like objects and all that. So...
Harold Jools
Greenhorn

Joined: Mar 16, 2005
Posts: 17
I fear that this will be a really stupid question, but here it goes:
How do I run this in JBuilder X? I've been able to run all my other assignments just by right clicking on the file name and selecting "run using defaults". But why can't I do that now?
I need to run it to see that it's woking...
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
Sorry I don't have any experience with JBuilder, I use WSAD. Have you tried running it from the command line? What happens when you do try to run it from JBuilder? What does the console tell you?
[ March 18, 2005: Message edited by: Hentay Duke ]
Harold Jools
Greenhorn

Joined: Mar 16, 2005
Posts: 17
I can't even find somewhere that I can even attempt ro run it.
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
That is probably because it does not have a:

public static void main(String[] args)

method in it. That is the entry point for a Java app.

I would recommend for a beginner to use a basic text editor, like Textpad, and the command line tools for compiling and running.
Harold Jools
Greenhorn

Joined: Mar 16, 2005
Posts: 17
I'm sorry for being so thick, but where do I find the command line tools?
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
If you are running Windows, you just open a DOS prompt (or click on the Start button, go to "Run..." and type "cmd"). When you see "C:\>", type "javac" and see what happens.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Harold Jools
Greenhorn

Joined: Mar 16, 2005
Posts: 17
I did that. Nothing happened.
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Something happened. Usually you will get a message telling you that javac is not found. You must add javac to your PATH, or cd your way to bin directory of where you have Java installed. Then you would run javac Triangle.java. Of course you would have to type in the location of Triangle.java so it would be more like
javac C:/Triangle.java

Something you are probably going to miss. You now have the class called Triangle. It has a few methods you want to use. You can't just run a class, you have to use a class. In the
public static void main(String[] args)
method you would put code that looks something like:


See, now that you have created this class you can use it.
Harold Jools
Greenhorn

Joined: Mar 16, 2005
Posts: 17
When I try this code in public static void main(String[] args)

I get an error message on setBase and setHeight saying: "cannot resolve symbol: method setBase (int)in class lab5.Triangle".
Why is that?
[ March 20, 2005: Message edited by: Harold Jools ]
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
You must not have put these methods in your class. You would need to add methods called setBase and setHeight that set the base and height.
Ben Wood
Ranch Hand

Joined: Aug 14, 2001
Posts: 342
Harold,

just to clarify, "cannot resolve symbol" basically means the Java compiler (javac) is saying "I can't see that". This could be because it doesn't exist, or it is out of reach somewhere. In your case these methods simply don't exist because you haven't written them. So, for Java to find setBase() you would need to define it as a method in Triangle.java



...now javac will be able to find it.

By the way, I agree with the earlier comment. If you try to use JBuilder or any other IDE you may end up confusing yourself...you'll be trying to learn Java AND how to use the IDE. Stick to textpad/a text editor.
Harold Jools
Greenhorn

Joined: Mar 16, 2005
Posts: 17
Here's what I've done now...the only thing left is a get method, if I'm not mistaken. Where do I put that and what would it look like?
By the way, thank you for all your help guys. I think I�m beginning to understand all this about objects and classes and all that now.



Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
your getX methods should go right were your setX methods are in the triangle class. Getters and setters take to basic form of:


I would not put a set for the area as that is a calculated variable, leaving that open to modification would allow a user of the Triangle class to have a Triangle where the area does not match the height and base.

Also you are going to have a problem with the area being an int. Google for 'Integer Division' and see if you can find out why.
[ March 23, 2005: Message edited by: Steven Bell ]
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
You might also think about the access to your local variables. Should they have default/package access or should they be private. Read this to understand which decision to make.
Harold Jools
Greenhorn

Joined: Mar 16, 2005
Posts: 17
I think I can manage with the first assignment now. Thanks for all the help. However, the second one, I feel that I need some help with now. I don't know if I'm even on the right lines here, so I would appreciate it if you could at least tell me if I'm doing it all wrong or not. Any help is greatly appreciated. Thanks again fellas.
By the way, before you say anything, my teacher hasn't made any contact with me for two weeks now. Don't know if it's the webmail that's messed up or what. But anyway, I can't go there for help. (Which I by the way hardly could before either, but still...)

Here's what I've done so far:



Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
When you create an instance of Employee you only pass one argument.

But in the Employee class constructor you are expecting two arguments

So you need to add the String of the employee's name as a second argument on construction of your Employee instance. e.g.
You might also think about breaking the employee's name into firstname and surname. Going back to your initial post you have a template for what is required in the Employee class, I have filled it in a bit to help you on your way

Hope this helps
[ March 25, 2005: Message edited by: Nigel Browne ]
Harold Jools
Greenhorn

Joined: Mar 16, 2005
Posts: 17
Yeah, I tried this before: new Employee(100, "John Doe");
I forgot the citation marks though, so I got an error message and that. But at least I wasn't completely wrong.

Thank you very much for the help Nigel.
Harold Jools
Greenhorn

Joined: Mar 16, 2005
Posts: 17
Hmmm...should I even create an object with name and hourly wage? Isn't that what the user shall put in himself? Do you know what I mean?
How do I create an object with the name and wage that the user has put in?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I need help - basic stuff (I think)
 
Similar Threads
Greetings All!
Top Rate for Java Consulting today ?
Code problems
Code Help
condition to leave all your loops