Hey, I have a Java assignment which I'm stuck on, I'm not looking for anyone to do the assignment for me, I would just like a little help and guidance in the right direction, here are my classes, and what my problem is:
MyShape Class
package calculations; public class MyShape { public double getArea(double length) { return 0; }//end of getArea public double getBoundaryLength(double length) { return 0; }//end of getBoundaryLength
Circle Class - There is also a Triangle class and a Square class, same format just different calculations.
Main class - I used this for the last part of my assignment, this provides the user with a simple menu to enter a length and calculate a shape of their choice. I'm not sure if it is still needed
package calculations; import java.util.Scanner; public class Main { private static double boundaryLength; private static double area; private static double length; static MyShape myShape; public static Scanner scan = new Scanner(System.in); public static int Menu()//creates a menu for the user { System.out.println("\nSelect a Shape or Exit: \n"); System.out.println("1. Square"); System.out.println("2. Circle"); System.out.println("3. Triangle"); System.out.println("4. Exit"); int option; System.out.println("Enter choice: "); option = scan.nextInt(); return option; }// end menu public static void main(String[] args) //switch { int option = 0; boolean test = true; while (option != 4) { option = Menu(); switch(option) { case 1: myShape = new Square(); test = true; break; case 2: myShape = new Circle(); test = true; break; case 3: myShape = new Triangle(); test = true; break; case 4: System.out.println("\n---System Shutdown---\n"); test = false; break; default: System.out.println("\nInvalid option!"); test = false; break; }//end of switch if(test == true) { do { System.out.println("Enter the length: "); while (!scan.hasNextDouble()) { System.out.println("That is not a valid number!\n"); System.out.println("Enter the length: "); scan.next(); }//end of while loop length = scan.nextDouble(); if(length <= 0) { System.out.println("You must enter a positive number!\n"); }//end of if statement } while (length <= 0); boundaryLength = myShape.getBoundaryLength(length); area = myShape.getArea(length); System.out.println("\nBoundary Length = " +Math.round(boundaryLength)); System.out.println("Area = " +Math.round(area)); }//end of if statement }//end of while loop }//end of public }//end of class
MyControlPanel class
package calculations; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; public class MyControlPanel extends javax.swing.JPanel { static int min = 0; static int max = 100; static int initial = 0; JSlider jSlider; JLabel jLabel1; JLabel jLabel2; JLabel jLabel3; JTextField jText1; JTextField jText2; public MyControlPanel() { initComponents(); jSlider = new JSlider(JSlider.HORIZONTAL, min, max, initial); jSlider.setMajorTickSpacing(10); jSlider.setMinorTickSpacing(1); jSlider.setPaintLabels(true); jSlider.setPaintTicks(true); jSlider.setValue(70); jSlider.addChangeListener(new MyChangeAction()); jText1 = new JTextField("Displaying JSlider value"); jText1 = new JTextField(); jText2 = new JTextField(); jText1.setText(" "); jText2.setText(" "); jLabel1 = new JLabel(); jLabel2 = new JLabel(); jLabel3 = new JLabel(); jLabel1.setText("Shape Dimension"); jLabel2.setText("Boundary Length = "); jLabel3.setText("Area = "); this.add(jSlider) ; this.add(jLabel1) ; this.add(jLabel2) ; this.add(jText1) ; this.add(jLabel3) ; this.add(jText2) ; setLayout(new FlowLayout()); } public class MyChangeAction implements ChangeListener { @Override public void stateChanged(ChangeEvent ce) { int value = jSlider.getValue(); String str = Integer.toString(value); jLabel1.setText(str); } } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 400, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 300, Short.MAX_VALUE) ); }// </editor-fold> // Variables declaration - do not modify // End of variables declaration }
MyFrame class
package calculations; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class MyFrame extends javax.swing.JFrame implements ActionListener { MyShape myShape = new MyShape(); public MyFrame() { initComponents(); JMenuBar jMenuBar = new JMenuBar(); JMenu Shape = new JMenu(); Shape.setText("Shape"); JMenuItem Square = new JMenuItem(); Square.addActionListener(new MySquareAction()); JMenuItem Triangle = new JMenuItem(); Triangle.addActionListener(new MyTriangleAction()); JMenuItem Circle = new JMenuItem(); Circle.addActionListener(new MyCircleAction()); Square.setText("Square"); Triangle.setText("Triangle"); Circle.setText("Circle"); jMenuBar.add(Shape); Shape.add(Square); Shape.add(Triangle); Shape.add(Circle); setJMenuBar(jMenuBar); MyControlPanel controlPanel = new MyControlPanel(); setLayout(new FlowLayout()); this.add(controlPanel); } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 400, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 300, Short.MAX_VALUE) ); pack(); }// </editor-fold> public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see [url=http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html]How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)[/url] */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { @Override public void run() { new MyFrame().setVisible(true); } }); } // Variables declaration - do not modify // End of variables declaration @Override public void actionPerformed(ActionEvent e) { throw new UnsupportedOperationException("Not supported yet."); } public class MySquareAction implements ActionListener { @Override public void actionPerformed(ActionEvent e) { myShape = new Square(); } } public class MyTriangleAction implements ActionListener { @Override public void actionPerformed(ActionEvent e) { myShape = new Triangle(); } } public class MyCircleAction implements ActionListener { @Override public void actionPerformed(ActionEvent e) { myShape = new Circle(); } } }
program.jpg
This is what my program looks like so far.
When the slider is moved the label, Shape Dimension, changes to the value at which the slider is moved to.
The JMenu Shape, has three jMenuItems Circle, Square and Triangle. Lets say the circle was selected, the slider value would be used to calculate the boundary length and area of the circle, same for each of the other shapes.
Okay, I'm sorry for all the code! But my problem is, how can I use the value which is given from the slider to calculate the shapes boundary length and area. So that both the boundary length and area are shown in the text boxes, jText1 and jText2.
Any help would be appreciated, Thanks!