Results 1 to 20 of 29
Thread: Help on Homework

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 Autogenerated 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 popup 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 builtin constant called Math.PI and a builtin 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 popup 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.
 09092013, 06:37 PM #2
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  GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum  Come say hello!

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
 09092013, 09:02 PM #4
Re: Help on Homework
If you're forever cleaning cobwebs, it's time to get rid of the spiders.
 09102013, 09:53 AM #5Moderator
 Join Date
 Apr 2009
 Posts
 12,356
 Rep Power
 21
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 **

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 Autogenerated 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" +" " ); } }
 09102013, 10:23 PM #7Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 4,524
 Rep Power
 7
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,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part

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 Autogenerated 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; */ } }
 09132013, 04:15 PM #9Moderator
 Join Date
 Apr 2009
 Posts
 12,356
 Rep Power
 21
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); }
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.Please do not ask for code as refusal often offends.
** This space for rent **
 09132013, 04:23 PM #10Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 4,524
 Rep Power
 7
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,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part

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 Autogenerated 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; */ } }
 09132013, 04:35 PM #12Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 4,524
 Rep Power
 7
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,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 09132013, 04:38 PM #13Moderator
 Join Date
 Apr 2009
 Posts
 12,356
 Rep Power
 21
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 **
 09132013, 04:47 PM #14Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 4,524
 Rep Power
 7
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,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 09132013, 05:02 PM #15Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 4,524
 Rep Power
 7
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,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part

Re: Help on Homework
wait, so what i'm doing its pointless? the diameter is not being inputted correctly?

Re: Help on Homework
and what is wrong with the second square im drawing?
 09132013, 05:11 PM #18Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 4,524
 Rep Power
 7
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^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part

Re: Help on Homework
i see two squares and a circle?
 09132013, 05:25 PM #20Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 4,524
 Rep Power
 7
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;
Regards,
JimLast edited by jim829; 09132013 at 05:30 PM.
The Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads

Homework Help
By EZEd in forum New To JavaReplies: 3Last Post: 04102012, 07:11 PM 
Homework Help please!
By ghjk in forum New To JavaReplies: 5Last Post: 03192012, 03:44 AM 
I need some homework help
By brlau in forum New To JavaReplies: 9Last Post: 12202011, 08:25 PM 
Please Help with Homework
By theuser in forum Advanced JavaReplies: 2Last Post: 07302009, 03:37 PM 
help with homework
By pinkdiamondgail in forum Advanced JavaReplies: 7Last Post: 04072009, 01:34 AM
Bookmarks