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)
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.
Code:
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);
}
}
Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.
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
//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)
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
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;
}
}
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
>I suck at this game, can you give me some pointers?
Class declarations must not have parentheses. Drop them and it will probably stop underlining (and compile too ). So it should look like
Code:
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.
Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.
//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 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
Code:
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
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)
Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.
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))
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
Code:
//assuming that all values are valid except 0 or negative
public static boolean getValid(double side) {
if(side>0) return true;
return false.
}
Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.
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?
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
Code:
rightTriangle.getValid(some_double_value_here);
But if you make a rightTriangle object and try to use the getValid method from it like this
Code:
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.
Kids, kids... you tried your best, and you failed miserably; the lesson is: never try.
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.
Bookmarks