How to make an object in java to be called from outside Bluej -
i'm entering studing programming in college, , give me homework create 1 app in java actualy identify different types of triangles acording sides they'll receive.
i'm fine that, i'm trying understand , write part call others methods , show results outside bluej.
can gave me last method , explain me little bit how works?
i need see actual working code learn how , when make objects classes, , how connect methods ones.
the console returning me error: non-static variable base cannot referenced static context
update: returning me: '.class' espected
in part of code:
... public static void main (int args[]){ triangle obj1 = new *triangle(int[]* args); system.out.print(obj1.trianglecheck()); } }
note: parte between "" shows error exist, "" not part of actual code itself. note2: see after "update" see on actual context.
also, if of out there tell me how improve code woulde too, i'm new in object-oriented languajes.
here 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 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 + 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 = " + 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 = " + 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 la base"; res = var1; } } else { res = "eso no es un triángulo, pues los lados de un triángulo, no pueden ser sumados menores 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 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 + 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 = " + 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 = " + 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 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 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 improve question apreaciated too
thanks in advance
the reason it's giving error because global variables, if they're being accessed field has static
modifier, need static
too. rid of error, base
, ladoa
, , ladob
need static int
s:
// remove problems // error. static int base; static int ladoa; static int ladob;
edit:
problem you're facing .class
expected because of mis-interpretation of telling you.
you calling:
new triangle(arg, arg, arg);
when asks for:
new triangle(int[] args);
you need argument name (must int[]
passed):
new triangle(args);
Comments
Post a Comment