Break into Methods

I have a small program I'm trying to break into methods

Page 1 of 1

7 Replies - 886 Views - Last Post: 18 January 2010 - 10:13 PM Rate Topic: -----

#1 DJPlayer  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 73
  • Joined: 19-September 07

Break into Methods

Post icon  Posted 18 January 2010 - 08:11 PM

this is the simplified version of a small program I'm trying to break into another method. I wanted to break the area after the input line into methods but it keeps giving me nasty errors. I tried numerous different ways returning different values, using different syntax for calling the methods, trying void and int etc.. etc.. This is basically my 4th day using Java, I'm a c++ user and I keep trying to code this like a function which could be my issue. Any help? (The real code has error checking etc.. and several options but I stripped it down to make creating methods more simple)


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



Is This A Good Question/Topic? 0
  • +

Replies To: Break into Methods

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Break into Methods

Posted 18 January 2010 - 08:28 PM

do you want to change the output method? or create a new method?
if you want to make a new method, what exactly do you want the method to do?
Was This Post Helpful? 0
  • +
  • -

#3 DJPlayer  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 73
  • Joined: 19-September 07

Re: Break into Methods

Posted 18 January 2010 - 08:41 PM

View Postjapanir, on 18 Jan, 2010 - 07:28 PM, said:

do you want to change the output method? or create a new method?
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/.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Break into Methods

Posted 18 January 2010 - 08:50 PM

Actually if you want to code in Java better to get into the object concept

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();	
	} 
} 


Was This Post Helpful? 0
  • +
  • -

#5 DJPlayer  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 73
  • Joined: 19-September 07

Re: Break into Methods

Posted 18 January 2010 - 09:43 PM

View Postpbl, on 18 Jan, 2010 - 07:50 PM, said:

Actually if you want to code in Java better to get into the object concept

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

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Break into Methods

Posted 18 January 2010 - 09:54 PM

View PostDJPlayer, on 18 Jan, 2010 - 08:43 PM, said:

Will this method initialize the objects and construct and destruct just as in C++?

There is no destructor in Java... it is done for you

View PostDJPlayer, on 18 Jan, 2010 - 08:43 PM, said:

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++)

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);
Was This Post Helpful? 0
  • +
  • -

#7 DJPlayer  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 73
  • Joined: 19-September 07

Re: Break into Methods

Posted 18 January 2010 - 10:05 PM

Quote

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);


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

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Break into Methods

Posted 18 January 2010 - 10:13 PM

View PostDJPlayer, on 18 Jan, 2010 - 09:05 PM, said:

understood, thank you. What I meant by destructor was that it would destruct automatically on termination.

Java will do it for you

View PostDJPlayer, on 18 Jan, 2010 - 09:05 PM, said:

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 ;)

"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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1