I'm just entering studing Programming in college, and they give me like homework create one app in java than actualy identify the different types of triangles acording to the sides they'll receive.
I'm just fine with that, but now i'm trying to understand and write the part who call the others methods and show the results outside Bluej.
Can somebody gave me that last method and explain me a little bit how it works?
I really need to see some actual working code to learn how and when make objects from classes, and how to connect the methods to this ones.
The console is returning me this error: non-static variable base cannot be referenced from a static context
UPDATE: Now is returning me: '.class' espected
in this part of the code:
...
public static void main (int args[]){
Triangle obj1 = new *Triangle(int[]* args);
System.out.print(obj1.triangleCheck());
}
}
NOTE: the parte between "" is where it shows the error exist, the "" are not part of the actual code itself. NOTE2: See it after "UPDATE" to see it on it actual context.
Also, if some of you out there tell me how to improve my code woulde be good too, i'm new in object-oriented languajes.
Here is the code
import java.util.Scanner;
public class Triangle {
int base;
int ladoa;
int ladob;
String res = "";
public Triangle(int arg[]) {
Scanner Triangle = new Scanner( System.in );
System.out.println("Ingrese el tamaño de la Base del triángulo");
int base = Triangle.nextInt();
System.out.println("Ingrese el tamaño del lado A del triángulo");
int ladoa = Triangle.nextInt();
System.out.println("Ingrese el tamaño del lado B del triángulo");
int ladob = Triangle.nextInt();
}
public String triangleCheck(int base, int ladoa, int ladob) {
String baseb = Integer.toString(base);
String sidea = Integer.toString(ladoa);
String sideb = Integer.toString(ladob);
String var1 = "";
String var2 = "";
String var3 = "";
String var4 = "";
int lados=ladoa+ladob;
String ladoss = sidea + sideb;
if (base>=(ladoa+ladob)) {
if (lados==base) {
var1 = "Es un triángulo equilatero, pues sus lados y su base son todos iguales";
var2 = "Lado A + Lado B = " + ladoss;
var3 = "Base = " + baseb;
res = var1 + "\n" + var2 + "\n" + var3;
} else if (ladoa==ladob && lados < base || lados > base){
var1 = "Es un triánglo isósceles, pues dos de sus lados son iguales y su base es diferente";
var2 = "Lado A = " + ladoss;
var3 = "Lado B = " + ladoss;
var4 = "Base = " + baseb;
res = var1 + "\n" + var2 + "\n" + var3 + "\n" + var4;
} else if (ladoa > ladob || ladoa < ladob) {
var1 = "Es un triángulo escaleno, pues sus lados y la base son distintas";
var2 = "Lado A = " + ladoss;
var3 = "Lado B = " + ladoss;
var4 = "Base = " + baseb;
res = var1 + "\n" + var2 + "\n" + var3 + "\n" + var4;
} else {
var1 = "Eso no es un triángulo, pues los lados de un triángulo, no pueden ser sumados menores a la base";
res = var1;
}
} else {
res = "Eso no es un triángulo, pues los lados de un triángulo, no pueden ser sumados menores a la base";
}
return res;
}
public static void main (int args[]){
Triangle obj1 = new Triangle(base, ladoa, ladob);
System.out.print(obj1.triangleCheck());
}
}
UPDATE:
import java.util.Scanner;
public class Triangle {
static int base;
static int ladoa;
static int ladob;
String res = "";
public Triangle(int arg[]) {
Scanner Triangle = new Scanner( System.in );
System.out.println("Ingrese el tamaño de la Base del triángulo");
int base = Triangle.nextInt();
System.out.println("Ingrese el tamaño del lado A del triángulo");
int ladoa = Triangle.nextInt();
System.out.println("Ingrese el tamaño del lado B del triángulo");
int ladob = Triangle.nextInt();
}
public String triangleCheck(int base, int ladoa, int ladob) {
String baseb = Integer.toString(base);
String sidea = Integer.toString(ladoa);
String sideb = Integer.toString(ladob);
String var1 = "";
String var2 = "";
String var3 = "";
String var4 = "";
int lados=ladoa+ladob;
String ladoss = sidea + sideb;
if (base>=(ladoa+ladob)) {
if (lados==base) {
var1 = "Es un triángulo equilatero, pues sus lados y su base son todos iguales";
var2 = "Lado A + Lado B = " + ladoss;
var3 = "Base = " + baseb;
res = var1 + "\n" + var2 + "\n" + var3;
} else if (ladoa==ladob && lados < base || lados > base){
var1 = "Es un triánglo isósceles, pues dos de sus lados son iguales y su base es diferente";
var2 = "Lado A = " + ladoss;
var3 = "Lado B = " + ladoss;
var4 = "Base = " + baseb;
res = var1 + "\n" + var2 + "\n" + var3 + "\n" + var4;
} else if (ladoa > ladob || ladoa < ladob) {
var1 = "Es un triángulo escaleno, pues sus lados y la base son distintas";
var2 = "Lado A = " + ladoss;
var3 = "Lado B = " + ladoss;
var4 = "Base = " + baseb;
res = var1 + "\n" + var2 + "\n" + var3 + "\n" + var4;
} else {
var1 = "Eso no es un triángulo, pues los lados de un triángulo, no pueden ser sumados menores a la base";
var2 = "";
var3 = "";
var4 = "";
res = var1;
}
} else {
res = "Eso no es un triángulo, pues los lados de un triángulo, no pueden ser sumados menores a la base";
}
return res;
}
public static void main (int args[]){
Triangle obj1 = new Triangle(int[] args);
System.out.print(obj1.triangleCheck());
}
}
Any suggestion, idea, comment or something than improve the question would be much apreaciated too
Thanks in advance
Aucun commentaire:
Enregistrer un commentaire