Click to See Complete Forum and Search --> : java homework :)
susancbk
10-07-2004, 10:59 PM
ive been working on a basic assignment relating to classes.
asks for a java class with three private instance variables, as described below. The class should have all of the instance methods described below. my job is to type in the code for each of the methods. The method heading should EXACTLY match the headings below. i have to make sure the code match the names used by the instance variables and in the method headings. Side c should be updated every time that side a or side b is changed. so there is no setC() mutator.
I unfortunately am clueless what to do. I find I achieve more when I look at/learn from the answer rather then frustrate myself with a problem ill never solve.
any help would be greatly appreciated.
---
Instance Variables
double a; // height of the triangle
double b; // base of the triangle
double c; // hypotenuse of the triangle
Instance Methods
//default constructor
public rightTriangle()
//specific constructor
public rightTriangle(double s1, double s2)
//side a accessor
public double getA()
//side b accessor
public double getB()
//side c accessor
public double getC()
//side a mutator
public void setA(double s1)
//side b mutator
public void setB(double s2)
//Calculate the area of the triangle ½ ab
public double area()
//getValid – tells if the given side is valid
public static boolean getValid(double side)
HaganeNoKokoro
10-07-2004, 11:38 PM
Well, I hesitate to do your homework for you, but here's an example of some of the basics in action. It should give you most of what you need to do the assignment.
public class MyRectangle {
//instance variables
private int width;
private int height;
//constructor
public MyRectangle() {
width=0;
height=0;
}
//method to get width
public int getWidth() {
return width;
}
//method to set width
public void setWidth(int w) {
width=w;
height=2*width; //there is no method to set the height. always set to 2*width
}
//method to get height
public int getHeight() {
return height;
}
//method to get area
public int area() {
return (width*height);
}
}
susancbk
10-08-2004, 09:03 PM
its not really doing my homework for me; that would be useless to me. I need to learn this stuff for my own sake otherwise i won't know it for the exam. id ask my professor myself If I wasnt out of town on break.
thank you for the reply. I plan on looking at it thoroughly and trying to get an understanding and figure out the rest for myself. I spent an hour trying to look it over and it was like a foreign language to me. with some examples to go from now, itll make more sense and is usually how i learn the best.
so thanks again, and ill be back if I have any more questions :)
edited:
ok ive been working on it. this is about as far as I could get. im still not sure what to put for the final getValid statement. This would be easier if I had my textbook with me. thanks again for any help
-----
public class rightTriangle() {
private double a;
private double b;
private double c;
//default constructor
public rightTriangle () {
a=0;
b=0;
c=0;
}
//specific constructor
public rightTriangle(double s1, double s2)
dunno;
//side a accessor
public double getA() {
return a;
}
//side b accessor
public double getB()
return b;
}
//side c accessor
public double getC()
return c;
}
//side a mutator
public void setA(double s1) {
a=s1;
}
//side b mutator
public void setB(double s2)
b=s2;
}
//Calculate the area of the triangle ½ ab
public double area() {
return (.5*a*b);
}
//getValid – tells if the given side is valid
public static boolean getValid(double side)
}
}
HaganeNoKokoro
10-09-2004, 02:10 AM
I'm not really sure what the getValid(double side) method is supposed to do either. I think (but don't quote me on this) that it is supposed to return true if the argument is equal to one of the sides, otherwise false. If that's the case, then
public boolean getValid(double side) {
if(side==a || side==b || side==c) return true;
return false;
}
As for the rightTriangle(double s1, double s2) constructor
public rightTriangle(double s1, double s2) {
a=s1; //set a
b=s2; //set b
c=Math.sqrt((a*a)+(b*b)); //set c based on a and b
}
Also note that in your setA and setB methods, you should probably be re-calculating c (as above) to keep it up to date.
susancbk
10-09-2004, 11:23 AM
here's what ive got again (pasted below). NetBeans is underlining
public class rightTriangle(){ and the final closing }. I'm assuming its something else which is triggering it.
Im still a little fuzzy on what this actually does. i need the professor to give us a kind of big picture, plain english explanation.
Here's what ive interpreted: I've created a class which can be imported and used by another java program, in the same way you would import java.util to use scanners. In this class ive identified three sides of a triangle...and am trying to calculate the area. But obviousely im doing more; if I just wanted to calculate the area I could have just defined some variables and done a simple calculation. So how far off is my interpretation and what exactly is this doing?
I really appreciate the help you've been giving me, so thank you again.
----
public class rightTriangle() {
private double a;
private double b;
private double c;
//default constructor
public rightTriangle() {
a=4;
b=2;
c=3;
}
//specific constructor
public rightTriangle(double s1, double s2) {
a=s1; //set a
b=s2; //set b
c=Math.sqrt((a*a)+(b*b)); //set c based on a and b
}
//side a accessor
public double getA() {
return a;
}
//side b accessor
public double getB() {
return b;
}
//side c accessor
public double getC() {
return c;
}
//side a mutator
public void setA(double s1) {
a=s1;
c=Math.sqrt((a*a)+(b*b));
}
//side b mutator
public void setB(double s2) {
b=s2;
c=Math.sqrt((a*a)+(b*b));
}
//Calculate the area of the triangle ½ ab
public double area() {
return (.5*a*b);
}
//getValid – tells if the given side is valid
public static boolean getValid(double side) {
if(side==a || side==b || side==c) return true;
return false;
}
}
agent_x91
10-09-2004, 01:28 PM
Originally posted by HaganeNoKokoro
I think (but don't quote me on this) that it is supposed to return true if the argument is equal to one of the sides, otherwise false.
Sorry, I couldn't resist quoting you on it:D:D:D:D:D
HaganeNoKokoro
10-09-2004, 01:57 PM
Class declarations must not have parentheses. Drop them and it will probably stop underlining (and compile too :D). So it should look like
public class rightTriangle {
//instead of
public class rightTriangle() {
The whole point of creating classes is to encapsulate data and methods into a logical structure. The example of the rightTriangle class is kind of trivial, but there are many practical uses of object-oriented methodology. A lot of it really comes into play when you begin to deal with subclasses and interfaces.
susancbk
10-09-2004, 09:49 PM
excellent. only thing left is its saying that
//getValid – tells if the given side is valid
public static boolean getValid(double side) {
if(side==a || side==b || side==c) return true;
return false;
is an illegal start of an expression?
HaganeNoKokoro
10-10-2004, 01:23 AM
Is the getValid(double side) method really supposed to be static? Because if it is, then it doesn't do what I thought. But if it doesn't do that, then I don't know what it is supposed to do.
In case your wondering about the static thing, a static method belongs to the class, while a non-static one belongs to an instance of the class.
An example of a static method is Integer.parseInt(String), which takes a string and, if all characters in the string are digits 0-9, returns an integer of that value (otherwise it returns null and throws a NumberFormatException). You don't have to create an Integer object to call that method, since it is a static part of the class. If you tried to do this
Integer myInteger=new Integer(4);
int x=myInteger.parseInt("12345");
it wouldn't compile, because static methods cannot be accessed like member methods.
In the case of your rightTriangle class, I simply can't imagine what the getValid method would do. Is it possible you are supposed to pass it 3 values and check if they form a valid right triangle? in that case
public static boolean getValid(double s1, double s2, double s3) {
if(Math.sqrt((s1*s1)+(s2*s2))==s3) return true;
else if(Math.sqrt((s1*s1)+(s3*s3))==s2) return true;
else if(Math.sqrt((s2*s2)+(s3*s3))==s1) return true;
return false;
}
would be correct. Then you would be able to call that method without creating a rightTriangle object, just by using rightTriangle.getValid(insert values here)
susancbk
10-17-2004, 09:45 PM
ok almost done here.
here's what ive been able to deduce from the final getValid statement:
getvalid() - should take a given side value and return true
if the side is valid and false if the side is not a valid
side - setA and setB will call this method before setting the sides
but how is this done?
also for the default constructor..I can set a and b but I need to calculate c . im assuming this is done the same way that I set it in the specific constructor c=Math.sqrt((a*a)+(b*b))
any help is once again greatly appreciated.
HaganeNoKokoro
10-17-2004, 09:59 PM
I'm still not sure what is supposed to be meant by "valid side". I suppose negative values are invalid, but other than that pretty much anything goes for sides a and b, since any two side lengths can be used as perpendicular legs to form a right triangle. In that case, it is probably supposed to be static //assuming that all values are valid except 0 or negative
public static boolean getValid(double side) {
if(side>0) return true;
return false.
}
susancbk
10-18-2004, 09:01 PM
the solution you suggested first of
public static boolean getValid(double s1, double s2, double s3) {
if(Math.sqrt((s1*s1)+(s2*s2))==s3) return true;
else if(Math.sqrt((s1*s1)+(s3*s3))==s2) return true;
else if(Math.sqrt((s2*s2)+(s3*s3))==s1) return true;
return false;
seems the most logical, and even useful to me.
my code won't compile with this. is this because, as you mentioned before, static methods cannot be accessed like member methods?
HaganeNoKokoro
10-18-2004, 10:06 PM
I'm pretty sure that one is not the solution they're looking for, since you are supposed to call it when you use the setA and setB methods. The one in my last post seems more likely.
How are you trying to use it so that it will not compile? It should compile as long as you use rightTriangle.getValid(some_double_value_here);But if you make a rightTriangle object and try to use the getValid method from it like this rightTriangle rt = new rightTriangle();
rt.getValid(some_double_value_here);Then it will not compile, and the error message will look like "Can't access static method from non-static context" or something similar.
susancbk
10-19-2004, 08:03 PM
ok so checking for positive numbers is what i need to do.
my professor was able to elaborate more for me-
---------------------
"getvalid is sent only one side and returns true if it is valid (i.e. not negative) and returns false if it is not valid (i.e. negative).
Then, in setA you will use an if statement like:
if (getvalid(s1) {
update a
update c
}
--------------------
so ive updated my getValid to be what you originally said:
//getValid – tells if the given side is valid
public static boolean getValid(double side) {
if(side>0) return true;
return false;
}
and ive changed my setA and setB to the following :
//side a mutator
public void setA(double s1) {
if (getValid(s1)) {
a=s1;
c=Math.sqrt((a*a)+(b*b));
}
}
//side b mutator
public void setB(double s2) {
if (getValid(s2)) {
b=s2;
c=Math.sqrt((a*a)+(b*b));
}
}
and everything seems to finally make sense and work fine! THANK you so much for your help once again, you've made everything make sense and have helped me not go crazy in figuring this assignment out.
blastmaster2k2
06-21-2006, 11:12 PM
For java homework solutions visitwww.freewebs.com/codepalz (http://www.freewebs.com/codepalz) we'll help you when noone else seems to want to.