Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Another beginner to Java!

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    5
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Another beginner to Java!

    Hello All,

    I am hoping one of you Java Jedi's can help me with my code. Currently my code works and gives the end result, but I need to use a method perimeter() in a square class and then the other classes circle, rectangle, triangle should inherit it. I understand how to extend the classes, but I am unsure how to create the method perimeter() and have the other classes use it. also, I need to create a loop to continue to prompt until you exit. Can you help? i am desperate!! thx for any help!



    import javax.swing.*;
    import javax.swing.UIManager;

    public class square
    {


    public static void main(String[] args)
    {



    double sq;
    double cir;
    double tri;
    double rec;

    String shape2 = JOptionPane.showInputDialog("Enter shape you want to calculate the circumference for - Enter 1-square, 2-rectangle, 3-circle, 4-triangle, and 5-Exit. ");
    double shape = Double.parseDouble(shape2);


    if (shape ==1){


    String square2 = JOptionPane.showInputDialog("Enter numeric value in centimeters ");

    double square = Double.parseDouble(square2);

    sq= 4 * square;

    JOptionPane.showMessageDialog(null,
    "You selected Square \n Length of each side= " + square + " cm \n Area=" + sq +"cm");

    }

    if (shape ==2){


    String H2 = JOptionPane.showInputDialog("Enter numeric Length in centimeters ");
    double H = Double.parseDouble(H2);
    String L2 = JOptionPane.showInputDialog("Enter numeric Height in centimeters ");
    double L = Double.parseDouble(L2);

    rec= H + L * 2;

    JOptionPane.showMessageDialog(null,
    "You selected Rectangle \n Height= " + H + " cm \n Length= " + L + "cm \n Area=" + rec +"cm");
    }

    if (shape ==3){


    String circle = JOptionPane.showInputDialog("Enter method to calculate circumference of a circle, Enter 1-Diameter or 2-Radius ");
    double circle2 = Double.parseDouble(circle);

    String value = JOptionPane.showInputDialog("Enter numeric value in centimeters ");
    double value2 = Double.parseDouble(value);

    if (circle2 ==1){
    cir= 3.14 * value2;
    JOptionPane.showMessageDialog(null,
    "You selected Circle \n circumference = " + cir +"cm");
    }
    else {
    cir= 3.14 * (value2*2);
    JOptionPane.showMessageDialog(null,
    "You selected Circle \n circumference = " + cir +"cm");
    }}

    if (shape ==4){


    String triangle2 = JOptionPane.showInputDialog("Enter numeric value in centimeters");

    double triangle = Double.parseDouble(triangle2);

    tri= 3 * triangle;

    JOptionPane.showMessageDialog(null,
    "You selected Triangle \n Length of each side= " + triangle + " cm \n Area=" + tri +"cm");

    }

    if (shape ==5){

    System.exit(0);
    }

    System.exit(0);

    }}


  2. #2
    Member vanDarg's Avatar
    Join Date
    Jan 2011
    Location
    Chicago
    Posts
    65
    My Mood
    Mellow
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: Another beginner to Java!

    Please put your code in highlight tags

    import javax.swing.*;
    import javax.swing.UIManager;
     
    public class square
    {
     
     
    public static void main(String[] args)
    {
     
     
     
    double sq;
    double cir;
    double tri;
    double rec;
     
    String shape2 = JOptionPane.showInputDialog("Enter shape you want to calculate the circumference for - Enter 1-square, 2-rectangle, 3-circle, 4-triangle, and 5-Exit. ");
    double shape = Double.parseDouble(shape2);
     
     
    if (shape ==1){
     
     
    String square2 = JOptionPane.showInputDialog("Enter numeric value in centimeters ");
     
    double square = Double.parseDouble(square2);
     
    sq= 4 * square;
     
    JOptionPane.showMessageDialog(null,
    "You selected Square \n Length of each side= " + square + " cm \n Area=" + sq +"cm");
     
    }
     
    if (shape ==2){
     
     
    String H2 = JOptionPane.showInputDialog("Enter numeric Length in centimeters ");
    double H = Double.parseDouble(H2);
    String L2 = JOptionPane.showInputDialog("Enter numeric Height in centimeters ");
    double L = Double.parseDouble(L2);
     
    rec= H + L * 2;
     
    JOptionPane.showMessageDialog(null,
    "You selected Rectangle \n Height= " + H + " cm \n Length= " + L + "cm \n Area=" + rec +"cm");
    }
     
    if (shape ==3){
     
     
    String circle = JOptionPane.showInputDialog("Enter method to calculate circumference of a circle, Enter 1-Diameter or 2-Radius ");
    double circle2 = Double.parseDouble(circle);
     
    String value = JOptionPane.showInputDialog("Enter numeric value in centimeters ");
    double value2 = Double.parseDouble(value);
     
    if (circle2 ==1){
    cir= 3.14 * value2;
    JOptionPane.showMessageDialog(null,
    "You selected Circle \n circumference = " + cir +"cm");
    }
    else {
    cir= 3.14 * (value2*2);
    JOptionPane.showMessageDialog(null,
    "You selected Circle \n circumference = " + cir +"cm");
    }}
     
    if (shape ==4){
     
     
    String triangle2 = JOptionPane.showInputDialog("Enter numeric value in centimeters");
     
    double triangle = Double.parseDouble(triangle2);
     
    tri= 3 * triangle;
     
    JOptionPane.showMessageDialog(null,
    "You selected Triangle \n Length of each side= " + triangle + " cm \n Area=" + tri +"cm");
     
    }
     
    if (shape ==5){
     
    System.exit(0);
    }
     
    System.exit(0);
     
    }}

    Please indent also, please
    Last edited by vanDarg; April 21st, 2011 at 10:43 AM.
    "Everything should be made as simple as possible, but not simpler."
    Asking Questions for Dummies | The Java Tutorials | Java Coding Styling Guide

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Another beginner to Java!

    I can't really read your code because it's not indented (even with the code tags), but where are you stuck with this? What are you having problems with?

    Recommended reading: Overriding and Hiding Methods (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Junior Member
    Join Date
    Apr 2011
    Posts
    5
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Another beginner to Java!

    i figured it out. i appreciate the help and sorry for not showing my code correctly. i am new to this. many thx!!!!

Similar Threads

  1. Beginner java programmer!
    By chrisivey1980 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: April 23rd, 2011, 03:06 AM
  2. [SOLVED] Java beginner is confused....
    By truebluecougarman in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 27th, 2011, 08:50 AM
  3. Java Beginner Here!
    By j3nn42o in forum The Cafe
    Replies: 10
    Last Post: January 10th, 2011, 04:57 AM
  4. A java beginner needs a lot of help and ideas
    By vesa in forum Java Theory & Questions
    Replies: 1
    Last Post: May 24th, 2010, 09:46 PM
  5. Java Beginner
    By rannoune in forum Java Theory & Questions
    Replies: 3
    Last Post: December 25th, 2009, 03:30 AM