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 12 of 12

Thread: Please Help URGENT!!

  1. #1
    Junior Member
    Join Date
    Nov 2017
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please Help URGENT!!

    Hey there,

    For some reason when I run this code my output isn't what I want it to be.

    Can you please tell me how to fix it or what I can make better to make this code work and output the different shapes that I made a list for. I want the names, color and radius of the shape to display.

    Here is my code for all my different classes and the main program:

    --- Update ---

    /*
     * Shape Class
     */
    package nov4;
     
    import java.io.*;
    import java.util.*;
     
    /**
     *
     * @author gavinnagra
     */
    public class Shape {
        static String name;
     
        protected List<Circle> circles; 
     
        Shape(String name, List<Circle> circles) {
     
            this.name = name;
            this.circles = circles;
        }
     
        public List<Circle> display1() {
     
            return circles;
     
        }
     
       @Override
        public String toString() {
            return "Name: " + this.name;
        }
     
    }

    /*
     * Circle Class
     */
    package nov4;
     
    public class Circle {
        String name;
        String color;
        double radius;
     
        Circle(String name, String color, double radius) {
            this.name = name;
            this.color = color;
            this.radius = radius;
        }
     
        public void display() {
            System.out.println("Name: " + this.name + "\nRadius: " + this.radius +
                    "\nColor: " + this.color);
        }
     
      @Override
        public String toString() {
            return super.toString() + "\nRadius: " + this.radius;
        }
    }

     /*
     * MAIN PROGRAM
     */
    package nov4;
    import java.util.*;
     
    public class Nov4 {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
     
            Circle c1 = new Circle("One", "Red", 1);
            Circle c2 = new Circle("Two", "Blue", 2);
            Circle c3 = new Circle("Three", "Red", 3);
            Circle c4 = new Circle("Four", "Blue", 4);
     
            List <Circle> red_circles = new ArrayList<Circle>();
            red_circles.add(c1);
            red_circles.add(c3);
     
            List <Circle> blue_circles = new ArrayList<Circle>();
            blue_circles.add(c2);
            blue_circles.add(c4);
     
            Shape red = new Shape("Red", red_circles);
            Shape blue = new Shape("Blue", blue_circles);
     
            System.out.println(red_circles.toString());
     
     
        }
     
    }

    Please help urgently if you can this is really bothering me!

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    261
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Please Help URGENT!!

    You can achieve this by adding below codes in Class Nov4 without using Shape class

    for(Circle c: red_circles)
            {
                c.display();
            }
     
            for(Circle c: blue_circles)
            {
                c.display();
            }
    Whatever you are, be a good one

  3. #3
    Junior Member
    Join Date
    Nov 2017
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Re: Please Help URGENT!!

    Quote Originally Posted by John Joe View Post
    You can achieve this by adding below codes in Class Nov4 without using Shape class

    for(Circle c: red_circles)
            {
                c.display();
            }
     
            for(Circle c: blue_circles)
            {
                c.display();
            }
    But I want to override the toString method and use it to display the info I am trying to display above, can you help me with that?

  4. #4
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    261
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Please Help URGENT!!

    Can we modify the Circle and Shape class ?
    Whatever you are, be a good one

  5. #5
    Junior Member
    Join Date
    Nov 2017
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Re: Please Help URGENT!!

    Quote Originally Posted by John Joe View Post
    Can we modify the Circle and Shape class ?
    Yes of course, as long as the main idea of the program is kept.

    The idea is I want to be able to call the individual objects .toString method to display all the info of the circle information stored in the array list.

    Please help!

  6. #6
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    261
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Please Help URGENT!!

    Add this in Nov class

    Shape red = new Shape("Red", red_circles);
     red.toString();
     Shape blue = new Shape("Blue", blue_circles);
      blue.toString();

    Modify to String method in Shape class

    @Override
        public String toString() {
            cir = new Circle();
            for(Circle c: circles)
            {
               c.display();
            }   
            return "Name: " + this.name;
        }

    Don't forget to declare Circle c
    Whatever you are, be a good one

  7. #7
    Junior Member
    Join Date
    Nov 2017
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please Help URGENT!!

    Quote Originally Posted by John Joe View Post
    Add this in Nov class

    Shape red = new Shape("Red", red_circles);
     red.toString();
     Shape blue = new Shape("Blue", blue_circles);
      blue.toString();

    Modify to String method in Shape class

    @Override
        public String toString() {
            cir = new Circle();
            for(Circle c: circles)
            {
               c.display();
            }   
            return "Name: " + this.name;
        }

    Don't forget to declare Circle c
    I added that part you told me to add to Nov4 but when I add the other part to the Shape class it's giving me error on the line "cir = new Circle();" in the Shape class. How to fix this problem?

    This is my code now in each class:

    Circle Class
    package nov4;
    public class Circle {
    String name;
    String color;
    double radius;

    Circle(String name, String color, double radius) {
    this.name = name;
    this.color = color;
    this.radius = radius;
    }

    public void display() {
    System.out.println("Name: " + this.name + "\nRadius: " + this.radius +
    "\nColor: " + this.color);
    }

    @Override
    public String toString() {
    return super.toString() + "\nRadius: " + this.radius;
    }
    }


    Shape Class
    package nov4;

    import java.io.*;
    import java.util.*;

    public class Shape {
    static String name;

    protected List<Circle> circles;

    Shape(String name, List<Circle> circles) {

    this.name = name;
    this.circles = circles;
    }

    public List<Circle> display1() {

    return circles;

    }

    @Override
    public String toString() {
    cir = new Circle();
    for(Circle c: circles)
    {
    c.display();
    }
    return "Name: " + this.name;
    }
    }

    Main Nov4 Class
    package nov4;
    import java.util.*;

    public class Nov4 {

    public static void main(String[] args) {

    Circle c1 = new Circle("One", "Red", 1);
    Circle c2 = new Circle("Two", "Blue", 2);
    Circle c3 = new Circle("Three", "Red", 3);
    Circle c4 = new Circle("Four", "Blue", 4);

    List <Circle> red_circles = new ArrayList<Circle>();
    red_circles.add(c1);
    red_circles.add(c3);

    List <Circle> blue_circles = new ArrayList<Circle>();
    blue_circles.add(c2);
    blue_circles.add(c4);

    Shape red = new Shape("Red", red_circles);
    red.toString();
    Shape blue = new Shape("Blue", blue_circles);
    blue.toString();

    System.out.println(red_circles.toString());


    }

    }


    You can test it out by pasting it and trying to run it, you'll see the problem i'm faced with :-(

  8. #8
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    261
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Please Help URGENT!!

    Add this line in your circle class

    public Circle(){}
    Whatever you are, be a good one

  9. #9
    Junior Member
    Join Date
    Nov 2017
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please Help URGENT!!

    ok now what

  10. #10
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    261
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Please Help URGENT!!

    Quote Originally Posted by easports View Post
    ok now what
    Run your project and check the result
    Whatever you are, be a good one

  11. #11
    Junior Member
    Join Date
    Nov 2017
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please Help URGENT!!

    it won't run because the line u told me to add before is showing errors: "cir = new Circle();" in the Shape class if u look above

  12. #12
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    261
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Please Help URGENT!!

    1. Add Circle cir ; in your Shape class
    2. Add this line in your Circle class

    public Circle(){}
    Whatever you are, be a good one

Similar Threads

  1. urgent
    By haritha in forum The Cafe
    Replies: 1
    Last Post: February 27th, 2013, 10:13 AM
  2. Need urgent help regarding java word wrap function.. URGENT
    By coldice in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 16th, 2011, 05:43 AM
  3. Urgent!!!!!!!!!!!!!!!!
    By dinesh03188 in forum JDBC & Databases
    Replies: 3
    Last Post: May 14th, 2010, 01:38 AM
  4. Urgent Help Please :)
    By Jamie94 in forum Algorithms & Recursion
    Replies: 0
    Last Post: May 12th, 2010, 06:34 AM
  5. Replies: 1
    Last Post: May 4th, 2009, 06:30 AM