Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 10th, 2005, 9:52 PM   #1
monka
Newbie
 
Join Date: Feb 2005
Posts: 6
Rep Power: 0 monka is on a distinguished road
Simple help with java console app

This program is supposed to use public and private data in two class to calculate the areas of various shapes.... for some reason all the output is 0 and null ... I can't figure out why!

shapes.java
import java.awt.*;
import java.io.*;
import java.text.DecimalFormat;

public class shapes
{
	
public static void main(String[] args)
{		
		int g = 0;
		double a = 0;
		double b = 0;
		System.out.println("This program will determine the area of multiple shapes.");
		System.out.println("          ");
		menu(a, b, g);
		
}
	
public static double menu(double a, double b, int g)
{
	int choice = 0;
	String banana;
	
	System.out.println("Get the area of a:");
	System.out.println("\t1. Triangle");
	System.out.println("\t2. Rectangle");
	System.out.println("\t3. Circle");
	System.out.println("\t4. Pentagon");
	System.out.println("\t5. Hexagon");
	System.out.println("\t6. Octagon");
	System.out.println("\t7. Calculate and Output");
	System.out.println("\t8. End this Program");

	banana = getConsoleString();
	choice = Integer.parseInt(banana);
	
	switch(choice)
	{

	case 1:
	{
		System.out.println("Let's figure out the area of a triangle!");
		System.out.println("Please enter the length of the base: ");
		System.out.println("          ");
		banana = getConsoleString();
		a = Double.parseDouble(banana);
		System.out.println("Please enter the height: ");
		System.out.println("          ");
		banana = getConsoleString();
		b = Double.parseDouble(banana);
		g = 1;
	 
		menu(a, b, g);

	break;
	}

	case 2:
	{
		System.out.println("Let's figure out the area of a rectangle!");
		System.out.println("Please enter the length: ");
		System.out.println("          ");
		banana = getConsoleString();
		a = Double.parseDouble(banana);
		System.out.println("Please enter the width: ");
		System.out.println("          ");
		banana = getConsoleString();
		b = Double.parseDouble(banana);
	
		g = 2;
	 
		menu(a, b, g);	
	
	break;
	}

	
	case 3:
	{
		System.out.println("Let's figure out the area of a circle!");
		System.out.println("Please enter the radius: ");
		System.out.println("          ");
		banana = getConsoleString();
		a = Double.parseDouble(banana);
		b = 0;
		g = 3;
		
		menu(a, b, g);
	break;
	}	
	
	case 4:
	{
		System.out.println("Let's figure out the area of a pentagon!");
		System.out.println("Please enter the apothem: ");
		System.out.println("          ");
		banana = getConsoleString();
		a = Double.parseDouble(banana);
		System.out.println("Please enter the perimeter: ");
		System.out.println("          ");
		banana = getConsoleString();
		b = Double.parseDouble(banana);
		g = 4;
		
		menu(a, b, g);	
	break;
	}
	
	case 5:
	{
		System.out.println("Let's figure out the area of a hexagon!");
		System.out.println("Please enter the apothem: ");
		System.out.println("          ");
		banana = getConsoleString();
		a = Double.parseDouble(banana);
		System.out.println("Please enter the perimeter: ");
		System.out.println("          ");
		banana = getConsoleString();
		b = Double.parseDouble(banana);
		g = 5;
		menu(a, b, g);
	break;
	}
	
	case 6:
	{
		System.out.println("Let's figure out the area of a octagon!");
		System.out.println("Please enter the apothem: ");
		System.out.println("          ");
		banana = getConsoleString();
		a = Double.parseDouble(banana);
		System.out.println("Please enter the perimeter: ");
		System.out.println("          ");
		banana = getConsoleString();
		b = Double.parseDouble(banana);
	 	g = 6;
	 	menu(a, b, g);
	 	break;		
	}
	
	case 7:
	{
	    subcalc calc = new subcalc(a, b, g);//creates an instance of the constructor 	
	    break;
	}
	
	case 8:
	{
		System.out.print ("Buh Bye!");
	    return(0);
	}
	
}

return(0);//end of menu
	}
	
public double Output(double a, double b, int g, double area, String shape)
{
	String banana;	
	System.out.println ("The area of the " + shape + " is " + area + ".");	
	System.out.println();
	banana = getConsoleString();
	
	menu(a, b, g);
	return(0);
	
}
	
	public static String getConsoleString()
	{
	
	int noMoreInput = -1;
	char enterKeyHit = '\n';
	int InputChar;
	
	StringBuffer InputBuffer = new StringBuffer(30);

	try
	{
		InputChar = System.in.read();
		
		while (InputChar != noMoreInput)
		{
			if ((char) InputChar != enterKeyHit)
			{
				InputBuffer.append((char) InputChar);
			}
			else
			{
				InputBuffer.setLength(InputBuffer.length() -1);
				break;
			}
			InputChar = System.in.read();
		}
	}
	catch(IOException IOX)
	{
		System.err.println(IOX);
	}
	return InputBuffer.toString();	
	
}//end of method GetConsoleString()

	


}

subcalc.java
import java.awt.*;
import java.io.*;



public class subcalc extends shapes 
{
private double a;
private double b;
private int g;
private double area;
private String shape;

subcalc(double a, double b, int g)// constructor
{
		switch(g)
		{

			case 1:
			{
				areaTriangle();
				shape = "Triangle";
				break;
			}
			
			case 2:
			{
				areaRectangle();
				shape = "Rectangle";
				break;
			}
			
			case 3:
			{
				areaCircle();
				shape = "Circle";
				break;	
			}
			
			case 4:
			{
				areaPentagon();
				shape = "Pentagon";
				break;
			}
			
			case 5:
			{
				areaHexagon();
				shape = "Hexagon";
				break;	
			}
			
			case 6:
			{
				areaOctagon();	
				shape = "Octagon";
			}
			
		}

}


private double areaTriangle()
{
	area = .5 * (a * b);
	finalCalc();
	return(0);	
}



private double areaRectangle()
{
	area = a * b;
	finalCalc();
	return(0);
}


private double areaCircle()
{
	area = Math.PI * (a * a);
	finalCalc();
	return(0);	
}


private double areaPentagon()
{
	area = .5 * (a * b);
	finalCalc();
	return(0);
}

private double areaHexagon()
{
	area = .5 * (a * b);
	finalCalc();
	return(0);
}

private double areaOctagon()
{
	area = .5 * (a * b);
	finalCalc();
	return(0);
	
}

private double finalCalc() //go back to output!
{
	
	shapes orig = new shapes();
	
	
	orig.Output(a, b, g, area, shape);
	return(0);
}



}	// end of subcalc

Thanks again!
monka is offline   Reply With Quote
Old Mar 10th, 2005, 9:58 PM   #2
monka
Newbie
 
Join Date: Feb 2005
Posts: 6
Rep Power: 0 monka is on a distinguished road
It seems to be going through the private methods (constructors) backwards. For instance if I add a println to the finalCalc(), it will output that before it will output a println in the switch()!
monka is offline   Reply With Quote
Old Mar 14th, 2005, 4:13 PM   #3
monka
Newbie
 
Join Date: Feb 2005
Posts: 6
Rep Power: 0 monka is on a distinguished road
solved
monka is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:11 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC