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: User defined classes and loops

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default User defined classes and loops

    My code all works fine and I have a user defined class called Circle. When I type "y" or "Y" it will not re run the program.

    import java.util.Scanner;
    public class CircleTest
    {
        public static void main (String [] args)
        {
            String choice;
            do{
     
                Circle crcl1 = new Circle(5.5);
                Scanner scan = new Scanner(System.in);
                System.out.println("Please enter the radius of the circle: ");
                crcl1.setRadius(scan.nextDouble());
     
                System.out.println("\nThe radius of the circle is " + crcl1.getRadius());
                System.out.println("\nThe area of the circle is " + crcl1.getArea());
                System.out.println("\nThe perimeter of the circle is " + crcl1.getPerimeter());
     
                System.out.println("\n\nWould you like to perform another calculation? <Y for yes, N for no> ");
                choice = scan.next();
     
            }while(choice == "y" || choice == "Y");
        }
    }

    This is my user defined class:
    public class Circle
    {
        private double radius;
        private double area;
        private double perimeter;
        double PI = 3.14159;
     
        public Circle (double pRadius)
        {
            radius = pRadius;
        }
        public double getRadius()
        {
            return radius;
        }
        public double setRadius(double newRadius)
        {
            if(newRadius<0)
            {
                System.out.println("Radius cannot be negative.");
            }
            else
            {
                radius = newRadius;
            }
            return radius;
        }
        public double getArea()
        {
            area = PI * radius * radius;
            return area;
        }
        public double getPerimeter()
        {
            perimeter = 2 * PI * radius;
            return perimeter;
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,603
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: User defined classes and loops

    Add a println statement to print out the value of choice after it is read so you can see what the computer sees when it executes the while statement.

    You should use the equals() method for comparing String objects. The == operator is for primitives.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3

  4. #4
    Junior Member
    Join Date
    Feb 2013
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: User defined classes and loops

    I added the print out statement after the while statement and it did print what I input into choice onto the display. It just won't run the program again.

Similar Threads

  1. LinkedList with user defined types
    By muhammadabrar78 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 22nd, 2013, 09:15 PM
  2. User Defined Criteria
    By SimonTemplar in forum Java Theory & Questions
    Replies: 0
    Last Post: February 20th, 2013, 06:02 PM
  3. User-Defined Methods
    By ZippyShannon in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 28th, 2011, 10:23 PM
  4. User Defined Methods
    By mgutierrez19 in forum Object Oriented Programming
    Replies: 11
    Last Post: October 20th, 2009, 06:57 PM