***I tried searching but I just can't understand what comes up. Sorry.
I am working on a server/client project for school. I am at my wit's end and I am about to give up. I do not have notes from class so I need some help. First I will post the directions I was given, then my current code, then my problem.
SERVER DIRECTIONS:
The Triangle class must have the following instance variables: side_a, side_b, side_c. Create a static variable to keep track of the number of Triangle objects created. Also create a static variable to hold the total of the perimeters of all Triangle objects created.
A three parameter constructor of the Triangle class should assign a trio of input values to the instance variables of the object. If not a valid triangle set all of the sides to 1. Create a separate private method called isValid(). The sum of any two sides of a triangle must be greater than the third in order to represent a valid triangle. No side may be 0 or negative. The constructor should also add 1 to the count and also call a method to calculate and then add the perimeter for that object to an accumulator.
The Triangle class must have the following methods as stated above:
public String toString() – returns the values for the 3 sides of the Triangle
You should also add a method to the Triangle class called calc_perim. This method will use the sides of the Triangle object to calculate the perimeter for that object.
SERVER CODE:
public class Triangle {
private int side_a, side_b, side_c;
private static int count;
**//PROBLEM 1: Java tells me 'perim' is not used.**
private static int perim;
private boolean valid;
public Triangle(int s1, int s2, int s3)
{
side_a = s1; side_b = s2; side_c = s3;
**//PROBLEM 2: Java tells me 'v' is not used.**
boolean v = isValid();
if (v = false)
{side_a = 1; side_b = 1; side_c = 1;}
Triangle.count++;
calc_perim(s1,s2,s3);
addTotalPerim();
reduceTotalPerim();
}
private int calc_perim()
{
int perimeterCalc = side_a + side_b + side_c;
return perimeterCalc;
}
private void addTotalPerim()
{
Triangle.perim += calc_perim();
}
private void reduceTotalPerim()
{
Triangle.perim -= calc_perim();
}
private boolean isValid()
{
boolean valid1;
if (side_a < 1)
{ valid1 = false;}
else if (side_b < 1)
{ valid1 = false;}
else if (side_c < 1)
{ valid1 = false;}
else if ((side_a + side_b) < side_c || (side_a + side_b) == side_c)
{ valid1 = false;}
else
{ valid1 = true;}
return valid1;
}
public boolean is_right()
{
boolean right;
if (((side_a * side_a) + (side_b * side_b)) == (side_c * side_c))
right = true;
else
right = false;
return right;
}
public boolean is_isoc()
{
boolean isoc;
if (side_a == side_b)
isoc = true;
else if (side_a == side_c)
isoc = true;
else if (side_b == side_c)
isoc = true;
else
isoc = false;
return isoc;
}
public boolean is_equil()
{
boolean equil;
if (side_a == side_b && side_a == side_c)
equil = true;
else
equil = false;
return equil;
}
public boolean is_scal()
{
boolean scal;
if (side_a == side_b || side_a == side_c || side_b == side_c)
scal = false;
else
scal = true;
return scal;
}
public String toString()
{
return "Side 1: " + side_a + " Side 2: " + side_b + " Side 3: " + side_c;
}
}
Sorry about formatting but this site has a terrible way of formatting code, unless I'm misunderstanding something...
SERVER PROBLEMS:
What is the correct way to add/subtract the perimeter obtained via method calc_perim to varible perim? The directions say to call the calc_perim method in the constructor but I can't figure out how, so I just made it do its calculations on its own.
In the constructor, after calling method isValid(), why am I told by Java that variable 'v' is not used? Did I call isValid() incorrectly? >>>>> How do I call a method in the constructor? <<<<<
Other than that major issue, the server class works fine.
The "is not used" message from the Java compiler is technically a warning, not an error, so you could run your program even with the message still in effect if you really wanted to. But your instincts are correct- it's a bad idea to ignore those messages.
In this case, your code has a serious problem. You're calling v = false
, which means that you're assigning a value of false
to v
. Change it to v == false
. By using ==
, you're doing a comparison, which is what you really want.
Suggestions:
Within Triangle class, initialize the static variables.
private static int count = 0; private static int perim = 0;
Within Triangle constructor, change,
if (v = false)
to
if (v == false)
Change calc_perim as:
private void addTotalPerim() { perim += calc_perim(); } private void reduceTotalPerim() { perim -= calc_perim(); }
Why you call reduceTotalPerim() after addTotalPerim(), didn't get this clear.
isValid function should check all combinations like a+b>c, b+c>a, c+a>b, if any one fails should be invalid
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.