Question

Please write in dr java

Write two classes: One class draws a square, and an exception that occurs when the user puts in an invalid dimension. First download the driver and put it in your project o DO NOT ALTER THE DRIVER! . Write a class file called DimensionException that inherits from Exception . Create the following constructors o Default - calls the parents constructor and pass a message that it is an invalid dimension o A constructor that takes one string parameters that is passed to the parents constructor . Write a class file called Square that DOES NOT HAVE amain method . An attribute of this class is o Length - corresponding to the length of each side . Create the following constructors o Default - creates a 1xl square (or a single star) o One that has a integer parameter that corresponds to its length and width . Accessors and Mutators for each variable o MAKE SURE THE MUTATORS CHECK FOR VALID VALUES! o If the dimension is set improperly throw the DimensionException . Create the following Methods o draw - this method will draw a square of asterisks (*) by the given dimensions o getArea - returns the area of the square o getPerimeter - returns the perimeter of the squareHere is the driver:

import java.util.*;
public class SquareDriver {
        public static void main(String[] args)
        {
                Scanner keyboard = new Scanner(System.in);
                String input ="";
                System.out.println("Welcome to the easy square program");
                while(true)
                {
                        System.out.println("Enter the length of the side of a square or enter QUIT to quit");
                        try
                        {
                                input = keyboard.nextLine();
                                if(input.equalsIgnoreCase("quit"))
                                        break;
                                int length = Integer.parseInt(input);
                                Square s = new Square();
                                s.setLength(length);
                                s.draw();
                                System.out.println("The area is "+s.getArea());
                                System.out.println("The perimeter is "+s.getPerimeter());
                        }
                        catch(DimensionException e)
                        {
                                System.out.println(e.getMessage());
                        }
                        catch(Exception e)
                        {
                                System.out.println(e.getMessage());
                        }
                }
        }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1
//Square.java
public class Square {
    private int length;
    public void setLength(int length) {
        this.length = length;
    }

    public void draw() {
        for(int i = 0;i<length;i++){
            for(int j = 0;j<length;j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }

    public int getArea() {
        return length*length;
    }


    public int getPerimeter() {
        return 4*length;
    }
}

----------------------------------------------------------------------------------------------------------------------

//SquareDriver.java
import java.util.*;
public class SquareDriver {
    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);
        String input ="";
        System.out.println("Welcome to the easy square program");
        while(true)
        {
            System.out.println("Enter the length of the side of a square or enter QUIT to quit");
            try
            {
                input = keyboard.nextLine();
                if(input.equalsIgnoreCase("quit"))
                    break;
                int length = Integer.parseInt(input);
                Square s = new Square();
                s.setLength(length);
                s.draw();
                System.out.println("The area is "+s.getArea());
                System.out.println("The perimeter is "+s.getPerimeter());
            }
            catch(Exception e)
            {
                System.out.println(e.getMessage());
            }
        }
    }
}

Welcome to the easy square program Enter the length of the side of a square or enter QUIT to quit 弘弘弘弘弘 弘弘弘弘弘 弘弘弘弘弘 弘弘弘弘弘 The

Add a comment
Know the answer?
Add Answer to:
Please write in dr java Here is the driver: import java.util.*; public class SquareDriver { 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
  • This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static...

    This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static void main(String args[]) throws IOException { Scanner inFile = new Scanner(new File(args[0])); Scanner keyboard = new Scanner(System.in);    TwoDArray array = new TwoDArray(inFile); inFile.close(); int numRows = array.getNumRows(); int numCols = array.getNumCols(); int choice;    do { System.out.println(); System.out.println("\t1. Find the number of rows in the 2D array"); System.out.println("\t2. Find the number of columns in the 2D array"); System.out.println("\t3. Find the sum of elements...

  • // please i cant understand why this program isnot running HELP me please? import java.util.Scanner; public...

    // please i cant understand why this program isnot running HELP me please? import java.util.Scanner; public class String { public static void main(String[] args) { double Sside, Rlength, Rwidth, Tbase, Theight, Area, Tarea, Rarea; String input; Scanner keyboard = new Scanner(System.in); System.out.print("To Calculate the are of Square Enter 'Square', For Rectangle Enter 'Rectangle', For Triangle Enter 'Triangle'"); input = keyboard.nextLine(); if (input.equalsIgnoreCase("SQUARE")) { System.out.println("Square Side"); Sside = keyboard.nextInt(); Tarea = Sside * Sside; System.out.println("Side = " + Tarea ); }...

  • I need help running this code. import java.util.*; import java.io.*; public class wordcount2 {     public...

    I need help running this code. import java.util.*; import java.io.*; public class wordcount2 {     public static void main(String[] args) {         if (args.length !=1) {             System.out.println(                                "Usage: java wordcount2 fullfilename");             System.exit(1);         }                 String filename = args[0];               // Create a tree map to hold words as key and count as value         Map<String, Integer> treeMap = new TreeMap<String, Integer>();                 try {             @SuppressWarnings("resource")             Scanner input = new Scanner(new File(filename));...

  • Looking for some simple descriptive pseudocode for this short Java program (example italicized in bold directly...

    Looking for some simple descriptive pseudocode for this short Java program (example italicized in bold directly below): //Create public class count public class Count {     public static void main(String args[])     {         int n = getInt("Please enter an integer value greater than or equal to 0");                System.out.println("Should count down to 1");         countDown(n);                System.out.println();         System.out.println("Should count up from 1");         countUp(n);     }            private static void countUp(int n)     {...

  • ****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...

  • import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {              ...

    import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {               int total = 0;               int num = 0;               File myFile = null;               Scanner inputFile = null;               myFile = new File("inFile.txt");               inputFile = new Scanner(myFile);               while (inputFile.hasNext()) {                      num = inputFile.nextInt();                      total += num;               }               System.out.println("The total value is " + total);        } } /* In the first program, the Scanner may throw an...

  • Please provide the full code...the skeleton is down below: Note: Each file must contain an int...

    Please provide the full code...the skeleton is down below: Note: Each file must contain an int at the beginning, stating the number of records in the file. Afterwards, the number of records in the file will follow. Each record that follows will consist of a last name (String), and a gpa (double). However, to test the error handling of your program, the number of records will not always match the int value. All possible combinations should be tested. 1.) Prompt...

  • Why does my program generate [] when viewing all guests names? ----------------------------------------------- import java.util.*; public class...

    Why does my program generate [] when viewing all guests names? ----------------------------------------------- import java.util.*; public class Delete extends Info { String name; int Id; int del; public Delete() { } public void display() { Scanner key = new Scanner(System.in); System.out.println("Input Customer Name: "); name = key.nextLine(); System.out.println("Enter ID Number: "); Id = key.nextInt(); System.out.println("Would you like to Delete? Enter 1 for yes or 2 for no. "); del = key.nextInt(); if (del == 1) { int flag = 0; //learned...

  • In java netbean8.1 or 8.2 please. The class name is Stock. It has 5 private attributes...

    In java netbean8.1 or 8.2 please. The class name is Stock. It has 5 private attributes (name, symbol, numberOfShares, currentPrice and boughtPrice)and one static private attribute (numberOfStocks). All attributes must be initialized (name and symbol to “No name/ symbol yet” and numberOfShares, currentPrice and boughtPrice to 0. Create two constructors, one with no arguments and the other with all the attributes. (However, remember to increase the numberOfStocks in both constructors. Write the necessary mutators and accessors (getters and setters) for...

  • import java.util.Scanner; // TASK #1 Add the file I/O import statement here /** This class reads...

    import java.util.Scanner; // TASK #1 Add the file I/O import statement here /** This class reads numbers from a file, calculates the mean and standard deviation, and writes the results to a file. */ public class StatsDemo { // TASK #1 Add the throws clause public static void main(String[] args) { double sum = 0; // The sum of the numbers int count = 0; // The number of numbers added double mean = 0; // The average of the...

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