Page 1 of 2 12 LastLast
Results 1 to 20 of 29
Like Tree1Likes

Thread: Help on Homework

  1. #1
    Messi5454 is offline Member
    Join Date
    Sep 2013
    Posts
    21
    Blog Entries
    1
    Rep Power
    0

    Default Help on Homework

    Got this so far
    Java Code:
    import javax.swing.*;
    import javax.swing.JOptionPane;
    public class Hw2 {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		JOptionPane.showMessageDialog(null, "the diameter of the circle:" +" " );
    		JOptionPane.showMessageDialog(null, "the area of the circle:" +" "  );
    		JOptionPane.showMessageDialog(null, " the radius of the circle:" +" " );
    		JOptionPane.showMessageDialog(null, "the circumference of the circle:" +" " );
    		JOptionPane.showMessageDialog(null, "the area of the smallest square containing the circle" +" " );
    		JOptionPane.showMessageDialog(null, " the area of the largest square contained in the circle" +" " );
    
    	}
    
    }



    Create a Java program that asks the user to input the diameter of a circle. After receiving the value for the diameter, your program does each of the following:

    Prints the diameter of the circle (just echoing what the user entered).
    Prints the area of the circle.
    Prints the radius of the circle.
    Prints the circumference of the circle.
    Prints the area of the smallest square containing the circle.
    Prints the area of the largest square contained in the circle.
    Draws in a JFrame window an outline (not filled) graphic image of the two squares and the circle.
    Draws in another JFrame window a filled graphic image of the two squares and the circle, like the one below.
    The program must use a pop-up window (JOptionPane) to get the diameter value from the user, and must show all of its results in a message window (also JOptionPane).
    Things to remember:
    The comment area of your program should contain its purpose, your name on the "@author" line, and the date on the "@version" line.
    Real numbers should be represented as double.
    Java has a built-in constant called Math.PI and a built-in function called Math.sqrt(). For example:

    double x = 5.5;
    double y;
    y = Math.sqrt(x);

    which assigns to y the square root of x.
    In order to use Java's graphics for the windows, you must begin your program with

    import javax.swing.*;
    Because you are using a pop-up window for input, you will not need to use the Scanner class, so you will NOT need to import java.util.
    The following picture might help you visualize the problem: the smallest square that contains the circle is blue, and the largest square contained in the circle is yellow.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default Re: Help on Homework

    Did you have a question, or did you just want to post your homework?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Messi5454 is offline Member
    Join Date
    Sep 2013
    Posts
    21
    Blog Entries
    1
    Rep Power
    0

    Default Re: Help on Homework

    They haven't taught me anything about import javax.swing.*; or how to print the image of the circle or how to find the are of the smallest square in a circle? Our professor just kind of threw it at us. Need somewhere to start

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Help on Homework

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,938
    Rep Power
    19

    Default Re: Help on Homework

    Break it down.
    The first part is:
    "Create a Java program that asks the user to input the diameter of a circle."
    So write some code that does that and store the value in a double, since you are going to need to use that for the next parts.

    The write code that does each of the succeeding steps...don't bother looking at the later stuff. At the moment you have nothing, so just start to tick off some of the requirements.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Messi5454 is offline Member
    Join Date
    Sep 2013
    Posts
    21
    Blog Entries
    1
    Rep Power
    0

    Default Re: Help on Homework

    Okay so now how do i start to find the rest?
    How do i draw the circle and then find the area of the biggest square?

    Java Code:
    import javax.swing.*;
    public class Hw2 {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		String input = " ";
    		double radius = 0,circumfrence,area1,area2,areas3,diameter=0;
    		
    	
    				
    	input = JOptionPane.showInputDialog("Please enter the diameter of the circle");
    	diameter= Integer.parseInt( input );
    		
    		radius= diameter/2;
    		circumfrence= 2 * Math.PI * radius;
    		area1 =  Math.PI * Math.sqrt(radius);
    		
    		
    		
    		JOptionPane.showMessageDialog(null, "the diameter of the circle:" + input );
    		JOptionPane.showMessageDialog(null, "the area of the circle:" + area1 );
    		JOptionPane.showMessageDialog(null, " the radius of the circle:" + radius );
    		JOptionPane.showMessageDialog(null, "the circumference of the circle:" + circumfrence );
    		JOptionPane.showMessageDialog(null, "the area of the smallest square containing the circle" +" " );
    		JOptionPane.showMessageDialog(null, " the area of the largest square contained in the circle" +" " );
    
    	}
    		
    
    }

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: Help on Homework

    So let me get this straight. This is an intro course to Java. You haven't discussed graphics or JPanels or painting. And the instructor expects you to do the above assignment?

    How long has this course been going on and what exactly does the instructor do?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    Messi5454 is offline Member
    Join Date
    Sep 2013
    Posts
    21
    Blog Entries
    1
    Rep Power
    0

    Default Re: Help on Homework

    okay so the proffesor extended our due date and gave us this code for the image of the that this is suppose produce and told us to work from there.
    Im inputing all my measurments and integers and the image is not coming out like its suppose to be. Can anybody tel me what i'm doing wrong?
    Java Code:
    import java.awt.*;
    
    import javax.swing.*;
    
    
    // begining of the Gepmetry class
    // geometry class uses JFrame to draw the window and 
    // geometric shapes  
    public class Hw2  extends JFrame {
    
    //Variables to use for drawing 3 shapes.
    
    //variable for circle
    public double cir_x = 0;
    public double cir_y = 0;
    public double cir_width = 0;
    public double cir_height = 0;
    public Color cir_color = null;
    
    //variables for square1
    public double square_x1 = 0;
    public double square_y1 = 0;
    public double square_width1 = 0;
    public double square_height1 = 0;
    public Color square_color1 = null;
    
    //variables for square2
    public double square_x2 = 0;
    public double square_y2 = 0;
    public double square_width2 = 0;
    public double square_height2 = 0;
    public Color square_color2 = null;
    
    //constructor function
    //this function is executed once we define a variable of
    //type class geometry in main.
    //this function creates the canvas with size of 500 by 500.
    //you can change the size if its needed
    public Hw2() {
    
     setSize(new Dimension(500, 500));
     // program will be finished by closing the window 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     // make the window visible
     setVisible(true);
    }
    
    //This function draws the shapes on the window
    //and get called automatically ( no need to call it saparely)
    //this function uses the variables that are defined above. The value of the
    //variables are set in main file.
    public void paint(Graphics g) {
    
         // draws the rectangle without color 
    	if(square_color1 == null)
    	{
    	    g.drawRect((int)square_x1, (int)square_y1, (int)square_width1, (int)square_height1);
    		
    	}
    	else
    	{
            // draws rectangle with color 
    		g.setColor(square_color1);
    	    g.fillRect((int)square_x1, (int)square_y1, (int)square_width1, (int)square_height1);
    	}
    	
    
    	if (cir_color == null)
    	{
    		g.drawOval((int)cir_x, (int)cir_y, (int)cir_width, (int)cir_height);
    	}
    	else
    	{
    		g.setColor(cir_color);
    		g.fillOval((int)cir_x, (int)cir_y, (int)cir_width, (int)cir_height);
    	}
    
    
       
    	if (square_color2 == null)
    	{
    		    g.drawRect((int)square_x2, (int)square_y2, (int)square_width2, (int)square_height2);
    	}
    	else
    	{
    		   g.setColor(square_color2);
    		   g.fillRect((int)square_x2, (int)square_y2, (int)square_width2, (int)square_height2);
    
    	}
    }
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		String input = " ";
    		double radius = 0,circumfrence,area1,area2,area3,diameter=0;
    		
    	
    				
    	input = JOptionPane.showInputDialog("Please enter the diameter of the circle");
    	diameter= Integer.parseInt( input );
    	
    		radius= diameter/2;
    		area1 =  Math.PI * (radius *radius);
    		area2 = diameter * diameter;
    		area3 = .5 * (radius * radius);
    		radius= diameter/2;
    		circumfrence= Math.PI * diameter;
    		area1 =  Math.PI * (radius *radius);
    		;
    		
    		
    		JOptionPane.showMessageDialog(null, "the diameter of the circle:" + input );
    		JOptionPane.showMessageDialog(null, "the area of the circle:" + area1 );
    		JOptionPane.showMessageDialog(null, " the radius of the circle:" + radius );
    		JOptionPane.showMessageDialog(null, "the circumference of the circle:" + circumfrence );
    		JOptionPane.showMessageDialog(null, "area of the smallest square containing the circle:" + area2 );
    		JOptionPane.showMessageDialog(null, "area of the largest square contained the circle:" + area3 );
    		
    		// defining variable filledObjects as type of Geometry
    	     Hw2 filledObjects =   new Hw2();
    	      
    	      // set the coordinates and width and heights 
    	      // of the shapes
    	      filledObjects.square_x1 = 175;
    	      filledObjects.square_y1 = 175;
    	      filledObjects.square_width1 = diameter;
    	      filledObjects.square_height1 = diameter;
    	      filledObjects.square_color1 = Color.yellow;
    	      
    	      filledObjects.cir_width = area3/2;
    	      filledObjects.cir_height = area3/236;
    	      filledObjects.cir_x = 175;
    	      filledObjects.cir_y = 175;
    	      filledObjects.cir_color = Color.blue;
    	      
    	      filledObjects.square_x2 = 175 + 22;
    	      filledObjects.square_y2 = 175 + 22;
    	      filledObjects.square_width2 = diameter;
    	      filledObjects.square_height2 =diameter;
    	      filledObjects.square_color2 = Color.red;
    	      
    
    
    	     /* 
    	      Geometry emptyObjects =   new Geometry();
    	      emptyObjects.cir_width = 150;
    	      emptyObjects.cir_height = 150;
    	      emptyObjects.cir_x = 30;
    	      emptyObjects.cir_y = 30;
    	      emptyObjects.cir_color = null;
    	      
    	      emptyObjects.square_x1 = 30;
    	      emptyObjects.square_y1 = 30;
    	      emptyObjects.square_width1 = 100;
    	      emptyObjects.square_height1 = 100;
    	      emptyObjects.square_color1 = null;
    	      */
    	     
    	
    }
    }

  9. #9
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,938
    Rep Power
    19

    Default Re: Help on Homework

    Java Code:
    public Hw2() {
        setSize(new Dimension(500, 500));  
    // program will be finished by closing the window
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    // make the window visible
        setVisible(true); 
    }
    You are displaying the frame before you set the values, so it uses the default value (0).
    I think you probably want to move all this code out into some sort of "display" method, and then call that once you have set all the values.
    gimbal2 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: Help on Homework

    In your paint routine you need to do super.paint(g) as the first thing.

    However, I would recommend extending JPanel and not JFrame and
    overriding paintComponent() instead of drawing directly into JFrame. Then add the JPanel instance to an instance of JFrame.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  11. #11
    Messi5454 is offline Member
    Join Date
    Sep 2013
    Posts
    21
    Blog Entries
    1
    Rep Power
    0

    Default Re: Help on Homework

    thank you but i have no idea what that meant.I know i dont sound very intellegent but As i said before our instructor didn't really teach us anything. I did however moved some codes around and now the only thing that doesnt work is my circle being contained in the square.
    Java Code:
    import java.awt.*;
    
    import javax.swing.*;
    
    
    // begining of the Gepmetry class
    // geometry class uses JFrame to draw the window and 
    // geometric shapes  
    public class Hw2  extends JFrame {
    
    //Variables to use for drawing 3 shapes.
    
    //variable for circle
    public double cir_x = 0;
    public double cir_y = 0;
    public double cir_width = 0;
    public double cir_height = 0;
    public Color cir_color = null;
    
    //variables for square1
    public double square_x1 = 0;
    public double square_y1 = 0;
    public double square_width1 = 0;
    public double square_height1 = 0;
    public Color square_color1 = null;
    
    //variables for square2
    public double square_x2 = 0;
    public double square_y2 = 0;
    public double square_width2 = 0;
    public double square_height2 = 0;
    public Color square_color2 = null;
    
    //constructor function
    //this function is executed once we define a variable of
    //type class geometry in main.
    //this function creates the canvas with size of 500 by 500.
    //you can change the size if its needed
    public Hw2() {
    
     setSize(new Dimension(500, 500));
     // program will be finished by closing the window 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     // make the window visible
     setVisible(true);
    }
    
    //This function draws the shapes on the window
    //and get called automatically ( no need to call it saparely)
    //this function uses the variables that are defined above. The value of the
    //variables are set in main file.
    public void paint(Graphics g) {
    
         // draws the rectangle without color 
    	if(square_color1 == null)
    	{
    	    g.drawRect((int)square_x1, (int)square_y1, (int)square_width1, (int)square_height1);
    		
    	}
    	else
    	{
            // draws rectangle with color 
    		g.setColor(square_color1);
    	    g.fillRect((int)square_x1, (int)square_y1, (int)square_width1, (int)square_height1);
    	}
    	
    
    	if (cir_color == null)
    	{
    		g.drawOval((int)cir_x, (int)cir_y, (int)cir_width, (int)cir_height);
    	}
    	else
    	{
    		g.setColor(cir_color);
    		g.fillOval((int)cir_x, (int)cir_y, (int)cir_width, (int)cir_height);
    	}
    
    
       
    	if (square_color2 == null)
    	{
    		    g.drawRect((int)square_x2, (int)square_y2, (int)square_width2, (int)square_height2);
    	}
    	else
    	{
    		   g.setColor(square_color2);
    		   g.fillRect((int)square_x2, (int)square_y2, (int)square_width2, (int)square_height2);
    
    	}
    }
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		String input = " ";
    		double radius = 0,circumfrence,area1,area2,area3,diameter=0;
    		
    	
    				
    	input = JOptionPane.showInputDialog("Please enter the diameter of the circle");
    	diameter= Integer.parseInt( input );
    	
    		radius= diameter/2;
    		area1 =  Math.PI * (radius *radius);
    		area2 = diameter * diameter;
    		area3 = .5 * (radius * radius);
    		radius= diameter/2;
    		circumfrence= Math.PI * diameter;
    		area1 =  Math.PI * (radius *radius);
    		;
    		
    		
    		JOptionPane.showMessageDialog(null, "the diameter of the circle:" + input );
    		JOptionPane.showMessageDialog(null, "the area of the circle:" + area1 );
    		JOptionPane.showMessageDialog(null, " the radius of the circle:" + radius );
    		JOptionPane.showMessageDialog(null, "the circumference of the circle:" + circumfrence );
    		JOptionPane.showMessageDialog(null, "area of the smallest square containing the circle:" + area2 );
    		JOptionPane.showMessageDialog(null, "area of the largest square contained the circle:" + area3 );
    		
    		// defining variable filledObjects as type of Geometry
    	     Hw2 filledObjects =   new Hw2();
    	      
    	      // set the coordinates and width and heights 
    	      // of the shapes
    	      filledObjects.square_x1 = 175;
    	      filledObjects.square_y1 = 175;
    	      filledObjects.square_width1 =Math.sqrt(area2);
    	      filledObjects.square_height1 = Math.sqrt(area2);
    	      filledObjects.square_color1 = Color.yellow;
    	      
    	      filledObjects.cir_width =diameter;
    	      filledObjects.cir_height = diameter;
    	      filledObjects.cir_x = 175;
    	      filledObjects.cir_y = 175;
    	      filledObjects.cir_color = Color.blue;
    	      
    	      filledObjects.square_x2 = 175 + 22;
    	      filledObjects.square_y2 = 175 + 22;
    	      filledObjects.square_width2 = Math.sqrt(area3);
    	      filledObjects.square_height2 =Math.sqrt(area3);
    	      filledObjects.square_color2 = Color.red;
    	      
    
    
    	     /* 
    	      Geometry emptyObjects =   new Geometry();
    	      emptyObjects.cir_width = 150;
    	      emptyObjects.cir_height = 150;
    	      emptyObjects.cir_x = 30;
    	      emptyObjects.cir_y = 30;
    	      emptyObjects.cir_color = null;
    	      
    	      emptyObjects.square_x1 = 30;
    	      emptyObjects.square_y1 = 30;
    	      emptyObjects.square_width1 = 100;
    	      emptyObjects.square_height1 = 100;
    	      emptyObjects.square_color1 = null;
    	      */
    	     
    	
    }
    }

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: Help on Homework

    Okay, for now don't worry about it. Just put super.paint(g) as the first statement in your paint method. That should allow the parent paint routine to fill in the background.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  13. #13
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,938
    Rep Power
    19

    Default Re: Help on Homework

    But he's drawing the Frame before any values have been set.
    I can't see how that will draw anything meaningful.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: Help on Homework

    Honestly, I didn't look carefully at what he was drawing. The OP just said things weren't displaying correctly. I just quickly tried out the code and recognized the symptom of not calling the parent paint method. But when I run the program, the squares are displayed and they vary with size according to the information I enter. And the Frame is not displayed until the dialogs display the computations. I will look further into this as I don't want to mislead the OP.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: Help on Homework

    @Tolls:

    You are correct! I am using Eclipse and there is no repainting of the frame after the values are set.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  16. #16
    Messi5454 is offline Member
    Join Date
    Sep 2013
    Posts
    21
    Blog Entries
    1
    Rep Power
    0

    Default Re: Help on Homework

    wait, so what i'm doing its pointless? the diameter is not being inputted correctly?

  17. #17
    Messi5454 is offline Member
    Join Date
    Sep 2013
    Posts
    21
    Blog Entries
    1
    Rep Power
    0

    Default Re: Help on Homework

    and what is wrong with the second square im drawing?

  18. #18
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: Help on Homework

    Also, all I see are two squares, no ovals. When I do a repaint I see the same thing.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  19. #19
    Messi5454 is offline Member
    Join Date
    Sep 2013
    Posts
    21
    Blog Entries
    1
    Rep Power
    0

    Default Re: Help on Homework

    i see two squares and a circle?

  20. #20
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: Help on Homework

    Then what problem were you reporting when you said the images weren't displaying correctly? I noticed that for some input values, the circles "width" is less than 1 which translates to zero when you draw the oval. Please post your most recent code.

    Edit: It was the circles height;
    Java Code:
      filledObjects.cir_height = area3/236;
    If area3 is less than 236, the height is effectively zero.

    Regards,
    Jim
    Last edited by jim829; 09-13-2013 at 05:30 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Page 1 of 2 12 LastLast

Similar Threads

  1. Homework Help
    By EZ-Ed in forum New To Java
    Replies: 3
    Last Post: 04-10-2012, 07:11 PM
  2. Homework Help please!
    By ghjk in forum New To Java
    Replies: 5
    Last Post: 03-19-2012, 02:44 AM
  3. I need some homework help
    By brlau in forum New To Java
    Replies: 9
    Last Post: 12-20-2011, 07:25 PM
  4. Please Help with Homework
    By theuser in forum Advanced Java
    Replies: 2
    Last Post: 07-30-2009, 03:37 PM
  5. help with homework
    By pinkdiamondgail in forum Advanced Java
    Replies: 7
    Last Post: 04-07-2009, 01:34 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •