import javax.swing.JOptionPane; // javax.swing = name of package
// JoptionPane = name of a class
public class hypotenuse1
{
public static void main(String[] args)
{
String input1, input2;
input1 = JOptionPane.showInputDialog ("Enter a integer value for the adjacent side of the triangle." );
input2 = JOptionPane.showInputDialog ("Enter a integer value for the opposite side of the triangle." );
int adj = Integer.parseInt(input1); // store values as ints
int opp = Integer.parseInt(input2);
double hyp = Math.sqrt(adj * adj + opp * opp);
int hyp1 = (int)hyp;
output(hyp1, opp, adj);
System.exit(0);
}
public static void output(int hyp1, int opp, int adj)
{
JOptionPane.showMessageDialog (null, "The hypotenuse of the triangle is " + hyp1 + "\n" +
"The Sin(x) is " + opp + "/" + hyp1 + "\n" +
"The Cos(x) is " + adj + "/" + hyp1 + "\n" +
"The Tan(x) is " + opp + "/" + adj + "\n" +
"The Csc(x) is " + hyp1 + "/" + opp + "\n" +
"The Sec(x) is " + hyp1 + "/" + adj + "\n" +
"The Cot(x) is " + adj + "/" + opp);
}
} // End of hypotenuse
Break into MethodsI have a small program I'm trying to break into methods
Page 1 of 1
7 Replies - 745 Views - Last Post: 18 January 2010 - 10:13 PM
#1
Break into Methods
Posted 18 January 2010 - 08:11 PM
Replies To: Break into Methods
#2
Re: Break into Methods
Posted 18 January 2010 - 08:28 PM
if you want to make a new method, what exactly do you want the method to do?
#3
Re: Break into Methods
Posted 18 January 2010 - 08:41 PM
japanir, on 18 Jan, 2010 - 07:28 PM, said:
if you want to make a new method, what exactly do you want the method to do?
no I'm trying to these lines into a method:
int adj = Integer.parseInt(input1); // store values as ints int opp = Integer.parseInt(input2); double hyp = Math.sqrt(adj * adj + opp * opp); int hyp1 = (int)hyp;
at least that seems like the most logical approach since this is the computation area.
Curious if I did the method correctly for the output also.. I toyed w/ syntax for a while till it worked properly. I want to make sure I'm not adding anything that I should not be doing. Not that easy swapping from C++ to Java. The more I play w/ the Java the more questions I come up w/.
#4
Re: Break into Methods
Posted 18 January 2010 - 08:50 PM
The main() method should be as short as possible, probably just to get user input. Usually it is the only static method in your class.
This is a little bit more object oriented
import javax.swing.JOptionPane;
public class Hypothenuse
{
int adj, opp;
double hyp;
int hyp1;
Hypothenuse(int adj, int opp) {
this.adj = adj;
this.opp = opp;
hyp = Math.sqrt(adj * adj + opp * opp);
hyp1 = (int)hyp;
}
void output() {
JOptionPane.showMessageDialog (null, "The hypotenuse of the triangle is " + hyp1 + "\n" +
"The Sin(x) is " + opp + "/" + hyp1 + "\n" +
"The Cos(x) is " + adj + "/" + hyp1 + "\n" +
"The Tan(x) is " + opp + "/" + adj + "\n" +
"The Csc(x) is " + hyp1 + "/" + opp + "\n" +
"The Sec(x) is " + hyp1 + "/" + adj + "\n" +
"The Cot(x) is " + adj + "/" + opp);
}
public static void main(String[] args) {
String input1, input2;
input1 = JOptionPane.showInputDialog ("Enter a integer value for the adjacent side of the triangle." );
input2 = JOptionPane.showInputDialog ("Enter a integer value for the opposite side of the triangle." );
int adj = Integer.parseInt(input1); // store values as ints
int opp = Integer.parseInt(input2);
Hypothenuse hypo = new Hypothenuse(adj, opp);
hypo.output();
}
}
#5
Re: Break into Methods
Posted 18 January 2010 - 09:43 PM
pbl, on 18 Jan, 2010 - 07:50 PM, said:
The main() method should be as short as possible, probably just to get user input. Usually it is the only static method in your class.
This is a little bit more object oriented
import javax.swing.JOptionPane;
public class Hypothenuse
{
int adj, opp;
double hyp;
int hyp1;
Hypothenuse(int adj, int opp) {
this.adj = adj;
this.opp = opp;
hyp = Math.sqrt(adj * adj + opp * opp);
hyp1 = (int)hyp;
}
void output() {
JOptionPane.showMessageDialog (null, "The hypotenuse of the triangle is " + hyp1 + "\n" +
"The Sin(x) is " + opp + "/" + hyp1 + "\n" +
"The Cos(x) is " + adj + "/" + hyp1 + "\n" +
"The Tan(x) is " + opp + "/" + adj + "\n" +
"The Csc(x) is " + hyp1 + "/" + opp + "\n" +
"The Sec(x) is " + hyp1 + "/" + adj + "\n" +
"The Cot(x) is " + adj + "/" + opp);
}
public static void main(String[] args) {
String input1, input2;
input1 = JOptionPane.showInputDialog ("Enter a integer value for the adjacent side of the triangle." );
input2 = JOptionPane.showInputDialog ("Enter a integer value for the opposite side of the triangle." );
int adj = Integer.parseInt(input1); // store values as ints
int opp = Integer.parseInt(input2);
Hypothenuse hypo = new Hypothenuse(adj, opp);
hypo.output();
}
}
thanx.. I was actually looking around a couple Java books I grabbed at the library and noticed info like this w/in the module/class area. Will this method initialize the objects and construct and destruct just as in C++? It's that 2nd to last line that gets me. "Hypotenuse hypto = new Hypotenuse(adj, opp);" basically you're calling a default constructor w/ parameters and initializing an object? (The syntax is a little different vs how I'd do it in C/C++)
This post has been edited by DJPlayer: 18 January 2010 - 09:45 PM
#6
Re: Break into Methods
Posted 18 January 2010 - 09:54 PM
DJPlayer, on 18 Jan, 2010 - 08:43 PM, said:
There is no destructor in Java... it is done for you
DJPlayer, on 18 Jan, 2010 - 08:43 PM, said:
I am not calling the default constructor, I am calling the constructor with 2 int parameters
Syntax is exactly the same than in C++ (but does not do the same thing) actually
Hypotenuse hypto = new Hypotenuse(adj, opp);
translated in C++ would be
Hypotheuse *hypto = &new Hypothenuse(adj, opp);
#7
Re: Break into Methods
Posted 18 January 2010 - 10:05 PM
Quote
Syntax is exactly the same than in C++ (but does not do the same thing) actually
Hypotenuse hypto = new Hypotenuse(adj, opp);
translated in C++ would be
Hypotheuse *hypto = &new Hypothenuse(adj, opp);
understood, thank you. What I meant by destructor was that it would destruct automatically on termination. I think I better start reading the module area of the book. I'm very interested in looking more depth at how Java handles pointers, since I've been fed for years on how Java's handling of pointers make things easier but takes away from it's power. C# next
This post has been edited by DJPlayer: 18 January 2010 - 10:07 PM
#8
Re: Break into Methods
Posted 18 January 2010 - 10:13 PM
DJPlayer, on 18 Jan, 2010 - 09:05 PM, said:
Java will do it for you
DJPlayer, on 18 Jan, 2010 - 09:05 PM, said:
"Pointer" is a forbidden word in Java world
Java actually has only pointers but they are hidden from the user
ClassA a = new ClassA();
is actually
ClassA *a = &new ClassA();
you just cannot create an Object in Java you can only create a pointer to an Object
A Java Vector is a Vector of pointers to Object not a Vector of Objects
And you cannot do mathematic operations on a pointer in Java
a++; is a no no won't even compile
|
|

New Topic/Question
Reply




MultiQuote



|