import javax.swing.JOptionPane; public class Driver { public static void main(St
ID: 3860663 • Letter: I
Question
import javax.swing.JOptionPane;
public class Driver {
public static void main(String[] args) {
Rectangle tennisCourt = null;
double width;
double length;
width = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter Length: "));
length = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter Width: " ));
tennisCourt.setLength(length);
tennisCourt.setWidth(width);
JOptionPane.showMessageDialog(null, "The area of a rectangle with a length "
+ "of " + getLength() + " and a width of " + getWidth() + " is " + getArea() );
}
}
public class Rectangle {
private double width;
private double length;
public Rectangle()
{
width = 0.0;
length = 0.0;
}
public Rectangle(double newWidth, double newLength)
{
width = newWidth;
length = newLength;
}
public void setWidth(double newWidth)
{
width = newWidth;
}
public void setLength(double newLength)
{
length = newLength;
}
public double getWidth()
{
return width;
}
public double getLength()
{
return length;
}
public double getArea()
{
return (length * width);
}
}
use the get member functions to display the area?
Explanation / Answer
import javax.swing.JOptionPane;
/**
*
* @author Sam
*/
public class RectangleDriver {
public static void main(String[] args) {
Rectangle tennisCourt = new Rectangle();
double width;
double length;
width = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter Length: "));
length = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter Width: "));
tennisCourt.setLength(length);
tennisCourt.setWidth(width);
JOptionPane.showMessageDialog(null, "The area of a rectangle with a length "
+ "of " + tennisCourt.getLength() + " and a width of " + tennisCourt.getWidth() + " is " + tennisCourt.getArea());
}
}
class Rectangle {
private double width;
private double length;
public Rectangle() {
width = 0.0;
length = 0.0;
}
public Rectangle(double newWidth, double newLength) {
width = newWidth;
length = newLength;
}
public void setWidth(double newWidth) {
width = newWidth;
}
public void setLength(double newLength) {
length = newLength;
}
public double getWidth() {
return width;
}
public double getLength() {
return length;
}
public double getArea() {
return (length * width);
}
}
Edited the code for your use!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.