Results 1 to 3 of 3
- 01-24-2011, 07:18 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 2
- Rep Power
- 0
help! shapes output and array issues!
Hi everyone, just need a bit of help here as ive been staring at this code for 4 days and not making much headway.
Also its apperantly a classic *drumrolls* area of shapes! with x,y location :D
Basicly in a nutshell its going to be an array of 20 (at the moment set at 2) of class shapes with subclass exented ( circle, square, retcangle and polygon)
with X,Y location. Also stuck on polygons :(
At the moment i think that i have made a uttermistake and now damaged the code. As now circle seems to provide one output while the square option puts out three output results!
Java Code:import javax.swing.*; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author Nick */ public class assignment { /** * @param args the command line arguments */ public static void main(String[] args) { // Create array of the abstract class Shape // This can contain the many different types of shape //remember to change the area back to 20! // also need to put the shapes into an array. Shape[] shapeArray = new Shape[2]; for (int i=0; i <shapeArray.length; i++){ String temp = JOptionPane.showInputDialog("Press key \n C for Circle,\n S for Square,\n R for Rectangle,\n P for Pologon"); //string displaying options to user //user input code if (temp.charAt(0)=='c'){ shapeArray[i] = new Circle(); double shapeArea = shapeArray[i].calculateArea(); System.out.println("area of the circle " + i + " is " + shapeArea); } else if(temp.charAt(0) =='s') shapeArray[i] = new Square(); double shapeArea = shapeArray[i].calculateArea(); System.out.println("area of the Square " + i + " is " + shapeArea); } } } //end of class assignment abstract class Shape { static String type = "Shape of unknown shape"; private double area; abstract double calculateArea(); } // End Shape Class class Square extends Shape { double side; Location topLeft; public Square(){; //user input screen String str_x =JOptionPane.showInputDialog ("Enter the center x coordinate"); String str_y =JOptionPane.showInputDialog ("Enter center Y coordiate"); double x = Double.parseDouble(str_x); double y = Double.parseDouble(str_y); topLeft = new Location (x,y); String str_rad =JOptionPane.showInputDialog("What is the length of the side"); side =Double.parseDouble(str_rad); } double calculateArea() { double area; area= side * side; return area; }} // End Square Class class Circle extends Shape { double radius; Location centre; public Circle(){; //where is the center String str_x =JOptionPane.showInputDialog ("Enter the center x coordinate"); String str_y =JOptionPane.showInputDialog ("Enter center Y coordiate"); double x = Double.parseDouble(str_x); double y = Double.parseDouble(str_y); centre = new Location (x,y); JOptionPane.showMessageDialog(null,"The new position of you recangle is:" + centre); String str_rad =JOptionPane.showInputDialog("What is the radius"); radius =Double.parseDouble(str_rad); } double calculateArea() { double area; area = 3.14 * ( radius * radius ); return ( area ); } }// end circle class class Rectangle extends Shape{ double length; double width; Location topleft; //need to fix! public Rectangle( double l, double w ) { length = l; width = w; String str_x =JOptionPane.showInputDialog ("Enter the x coordinate"); String str_y =JOptionPane.showInputDialog ("Enter Y coordiate"); double x = Double.parseDouble(str_x); double y = Double.parseDouble(str_y); topleft = new Location (x,y); JOptionPane.showMessageDialog(null,"The new position of you recangle is:" + topleft); String str_lenght =JOptionPane.showInputDialog("What is the lenght"); String str_width =JOptionPane.showInputDialog("What is the width"); length =Double.parseDouble(str_lenght); width =Double.parseDouble(str_width); } double calculateArea() { double area; area= length * width; return ( area ); }}// end rectangle class class Pologon extends Shape { //needs correct coding to be added on this! //polgon is an array of loction objects // think about the arry of the item example // double side; double x; double y; Location topLeft; public Pologon() {} double calculateArea() { double area; area= x * y; return area; } } // end pologon class class Location { double x; double y; void move (Location destination){ //code to move a location x=destination.x; y=destination.y; } double calculateDistance (Location other){ //code to calculate the distance double xdist; double ydist; double distance; // calculate the vertical and horizontal sides of the // triangle formed by the two points xdist = x-other.x; ydist = y-other.y; // Perform pythagoras theorm to calculate the hypotenuse // of the triangle formed by the two points distance = Math.sqrt(xdist * xdist + ydist * ydist); return distance; } public Location(double east, double north) { x=east; y=north; } } // End of location class
- 01-24-2011, 09:19 PM #2
Member
- Join Date
- Jan 2011
- Posts
- 2
- Rep Power
- 0
Right the arrays and else-if have been fixed braces issue {...} now having issue with location giving numbers out as 26c623af for example, and I don't know how to start the polygon
- 01-25-2011, 09:49 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 63
- Rep Power
- 0
Similar Threads
-
Array traversal issues
By sondratheloser in forum New To JavaReplies: 3Last Post: 08-13-2012, 12:49 AM -
Help with Array output Needed!
By 2potatocakes in forum New To JavaReplies: 2Last Post: 03-07-2009, 06:36 PM -
Array issues
By joka22 in forum New To JavaReplies: 6Last Post: 10-07-2008, 11:05 AM -
output from an array
By @eddie.com in forum New To JavaReplies: 5Last Post: 08-15-2008, 08:26 AM -
Array issues
By Neo82 in forum New To JavaReplies: 1Last Post: 12-31-2007, 03:22 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks