Question

package rectangle; public class Rectangle {    private int height;    private int width;    public...

package rectangle;

public class Rectangle {
   private int height;
   private int width;

   public Rectangle(int aHeight, int aWidth) {
   super();
   height = aHeight;
   width = aWidth;
   }

   public int getHeight() {
   return height;
   }

   public int getWidth() {
   return width;
   }

   public void setHeight(int aHeight) {
   height = aHeight;
   }

   public void setWidth(int aWidth) {
   width = aWidth;
   }

   public int computeArea() {
   return width * height;
   }

   public int compputeCircumference() {
   return 2 * (width + height);
   }
   }

package rectangle;

import java.util.Scanner;

public class RectangleApp {
   public static void main(String[] args) {
   Scanner sc = new Scanner(System.in);
   System.out.println("Enter height and width of Rectangle: ");
   Rectangle r = new Rectangle(sc.nextInt(), sc.nextInt());
   System.out.println("Area: "+r.computeArea());
   System.out.println("Circumference: "+r.compputeCircumference());
   }
   }

Currently, I have these two codes. One is to find the area of a rectangle, and the other is a driver. I have one error in the driver code, in the line " Scanner sc = new Scanner(System.in); ". the sc after Scanner is highlighted as incorrect, and my TA told me I didn't use the Scanner object "sc" you created. Can someone tell me how I can fix this?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Rectangle.java :

package rectangle;//package
//Java class
public class Rectangle {
   //instance variables
   private int height;
   private int width;
   //constructor with parameter
   public Rectangle(int aHeight, int aWidth) {
       super();
       height = aHeight;
       width = aWidth;
   }
   //getter methods
   public int getHeight() {
       return height;
   }

   public int getWidth() {
       return width;
   }
   //setter methods
   public void setHeight(int aHeight) {
       height = aHeight;
   }

   public void setWidth(int aWidth) {
       width = aWidth;
   }
   //method to computer area
   public int computeArea() {
       return width * height;//return area
   }
   //method to calculate circumference
   public int compputeCircumference() {
       return 2 * (width + height);//return circumference
   }
}

****************************************

RectangleApp.java :

package rectangle;//package
//import package
import java.util.Scanner;
//java class
public class RectangleApp {
   //entry point of the program, main() method
   public static void main(String[] args) {
       //object of Scanner class
       Scanner sc = new Scanner(System.in);
       //using try
       try {
           //asking height and width
           System.out.println("Enter height and width of Rectangle: ");
           //creating object of Rectangle and passing height and width
           Rectangle r = new Rectangle(sc.nextInt(), sc.nextInt());
           //method call to compute area
           System.out.println("Area: " + r.computeArea());
           //method call to compute Circumference
           System.out.println("Circumference: " + r.compputeCircumference());
       } finally {
           sc.close();//close Scanner
       }
   }
}

===============================

Screen 1:RectangleApp.java

Add a comment
Know the answer?
Add Answer to:
package rectangle; public class Rectangle {    private int height;    private int width;    public...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider the following declaration for a class that will be used to represent rectangles. public class...

    Consider the following declaration for a class that will be used to represent rectangles. public class Rectangle { private double height; private double width; public Rectangle() { height = 2.0; width = 1.0; } public Rectangle(double w, double h) { height = h; width = w; } public double getHeight() { return height; } public double getWidth() { return width; } public void setHeight(double h) { height = h; } public void setWidth(double w) { width = w; } //Other...

  • this is a jave program please help, I'm so lost import java.util.Scanner; public class TriangleArea {...

    this is a jave program please help, I'm so lost import java.util.Scanner; public class TriangleArea { public static void main(String[] args) { Scanner scnr = new Scanner(System.in);    Triangle triangle1 = new Triangle(); Triangle triangle2 = new Triangle(); // Read and set base and height for triangle1 (use setBase() and setHeight())    // Read and set base and height for triangle2 (use setBase() and setHeight())    // Determine larger triangle (use getArea())    private int base; private int height; private...

  • I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a...

    I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a void method named howToColor(). void howToColor(); } GeometricObject class: public class GeometricObject { } Sqaure class: public class Square extends GeometricObject implements Colorable{ //side variable of Square double side;    //Implementing howToColor() @Override public void howToColor() { System.out.println("Color all four sides."); }    //setter and getter methods for Square public void setSide(double side) { this.side = side; }    public double getSide() { return...

  • JAVA Modify the code to create a class called box, wherein you find the area. I...

    JAVA Modify the code to create a class called box, wherein you find the area. I have the code in rectangle and needs to change it to box. Here is my code. Rectangle.java public class Rectangle { private int length; private int width;       public void setRectangle(int length, int width) { this.length = length; this.width = width; } public int area() { return this.length * this.width; } } // CONSOLE / MAIN import java.awt.Rectangle; import java.util.Scanner; public class Console...

  • I need help with my java code i am having a hard time compiling it //...

    I need help with my java code i am having a hard time compiling it // Cristian Benitez import java.awt.Color; import java.awt.Graphics; import java.util.ArrayList; import javax.swing.JPanel; Face class public class FaceDraw{ int width; int height; int x; int y; String smileStatus; //default constructor public FaceDraw(){ } // Getters and Setters for width,height,x,y public int getWidth(){ return width; } public void setWidth(int width){ this.width=width; } public int getHeight(){ return height; } public void setHeight(int height){ this.height=height; } public int getX(){ return...

  • m The interface Measurable is defined as the following: /** An interface for methods that return...

    m The interface Measurable is defined as the following: /** An interface for methods that return the perimeter and area of an object. */ public interface Measurable { public double getPerimeter(); public double getArea(); } The class Rectangle implements the interface. The incomplete program is written as follows: 1 public class Rectangle 2 { private double width; private double height; public Rectangle(double w, double h) { width = w; height h; } { 3 4 5 6 7 8 9...

  • Consider the Rectangle2 java class definition below. Write the definition of an equals) method that checks...

    Consider the Rectangle2 java class definition below. Write the definition of an equals) method that checks if two Rectangle objects have the same dimensions Write a Java program to test the equals method public class Rectangle2 K private int width, length; public Rectangle2(int w, int 1) { setWidth(w); setLength(1); System.out.println("Inside parameterized!!!"); } public void setWidth(int w) { width = w;} public void setLength(int i) { length = 1;} int getWidth() { return width; } int getLength() { return length; }...

  • Why are obj1 and obj2 printing the same areas? I'm trying to learn about Comparable interface....

    Why are obj1 and obj2 printing the same areas? I'm trying to learn about Comparable interface. Couldn't figure it out. the compare to method should print 0, 1, or -1 import java.util.*; public class RectangleMain {    public static void main(String [] args) throws CloneNotSupportedException    {        /*ComparableRectangleAlsoCloneable obj1 = new ComparableRectangleAlsoCloneable(4, 5);        ComparableRectangleAlsoCloneable obj2 = (ComparableRectangleAlsoCloneable)obj1.clone();               System.out.println(obj1.toString());        System.out.println(obj1 == obj2); //false        System.out.println(obj2.toString());*/               Scanner...

  • ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class...

    ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used...

  • This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets...

    This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets and sets methods for the variables. Create a No-Arg constructor and a constructor that accepts all three values. At the end of the class add a main method that accepts variables from the user as input to represent the length and width of a room in feet and the price of carpeting per square foot in dollars...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT