Question

In: Computer Science

4. Modify the program geometryDemo to use the class basicGeometry and the threeSides code. Note from...

4. Modify the program geometryDemo to use the class basicGeometry and the threeSides code.

  1. Note from the comments in the main routine that it wants an input argument of 3 to solve for a triangle and 4 to solve for a square or rectangle.
  2. MODIFY the code to add another method in the 'threeSides' and 'fourSides' classes to return the perimeter. ASSUME the following for TRIANGLES.
    ** for AREA (1/2 * base * height) dimension1 is base, dimension2 is height
    ** for Volume (1/2 * base * height * depth or altitude) dimension1 is base, dimension2 is height and dimension3 is altitude
    ** for Perimeter, the 3 dimensions are the 3 sides.

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

geometryDemo Code

package com.java24hours;

/**
* geometryDemo to show class inheritance
* super class 'basicGeometry' is inherited by class 'fourSides'
* class fourSides is inherited by class 'threeSides'
*
*
*/
public class geometryDemo
{
public static void main(String[] args)
{
// Create the main object. the threeSides class includes all functions, so use that
// because threeSides inherits (extends) all of the 4 sides public data and methods
threeSides myGeometry = new threeSides();
  
  
//variables for calculation outputs
double outArea = 0;
double outVolume = 0;
  
//must have a numeric argument it it will fail
if (args.length == 0)
{
System.out.println("ERROR ** numeric input argument required: must be 3 (triangle) or 4 (square/rectangle)");
return;
}
//pass in number of sides... NOTE this will still crash if input is not a number
int acount = Integer.valueOf(args[0]);

//initialize all dimensions (firstDimension, secondDimension, thirdDimension)
// This is INSTEAD of initializing the class object as you have done before
// rather than passin the values in when you create the object like
// threSides myGeometry = new threeSides(12, 20, 4);
// a method is needed to sent in the initial data...

myGeometry.setDimensions(12, 20, 4);
//initialize the 4 sided only object
geom4Sides.setDimensions(12,20,4);
//determine which methods for area and volume to call based on number of sides
switch (acount)
{
case 3:
System.out.println("Triangle calculations:");
outArea = myGeometry.getTArea();
outVolume = myGeometry.getTVolume();

//need new call here to get perimeter,
// method added to 3 sdes, getTPerimeter

break;

case 4:
System.out.println("Square/rectangle calculations:");
outArea = myGeometry.getShapeArea();
outVolume = myGeometry.getVolume();

//let's see if we get the same values using the generic myGeometry that is
//class threeSides inheriting (extending) 4 sides, or the actual 4sides class
double fsArea = geom4Sides.getShapeArea();
double fsVolume = geom4Sides.getVolume();
System.out.println("Using the fourSides class, Area=" + fsArea + " Volume=" + fsVolume);
  
//need new call here to get perimeter,
// method added to 4 sides, getPerimeter

break;

default:
System.out.println("Invalid argument count = " + acount + " Pass in 3 or 4 arguments");
}

// Display the calculated area and volume.
System.out.println("Area=" + outArea + " Volume=" + outVolume);
}
}

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

basicGeometry Class Code

package com.java24hours;

public class basicGeometry
{
//length and width of initial shape; for triangle, length is height
double firstDimension;
double secondDimension;
double thirdDimension;

//initialize shape dimensions
public void setShape(double dim1, double dim2, double dim3)
{
firstDimension = dim1;
secondDimension = dim2;
thirdDimension = dim3;
}

}

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

threeSides Code

//triangle calculations inherit rectangle and main geometry classes
public class threeSides extends fourSides
{
//triangles cut square/rectangle measurements in half
double inHalf = .5;

//triangle area is half of rectangle area
public double getTArea()
{
//use the inherited method for area, and cut in half for triangle
return getShapeArea() * inHalf;
}

//triangle volume first 3 dimensions multiplied, then cut in half
public double getTVolume()
{
//use the inherited method for volume, and cut in half for triangle
return (firstDimension * secondDimension * thirdDimension * inHalf);
}

//triangle perimeter... add a new method to calculate the perimeter given the 3 dimensions
// remember, since setShape was called in the beginning, you have access to the
// data from the class variables firstDimension, secondDimension,

}

Solutions

Expert Solution

Here is the geometryDemo class:

import java.util.*;
import java.io.*;
public class geometryDemo extends threeSides
{
        public static void main(String[] args)
        {
                threeSides myGeometry = new threeSides();
                fourSides geom4Sides=new fourSides();
                double outArea = 0;
                double outVolume = 0;
                double outPerimeter=0;
                if (args.length == 0)
                {
                        System.out.println("ERROR ** numeric input argument required: must be 3 (triangle) or 4 (square/rectangle)");
                        return;
                }
                int acount = Integer.valueOf(args[0]);
                myGeometry.setDimensions(12, 20, 4);
                geom4Sides.setDimensions(12,20,4);
                switch (acount)
                {
                case 3:
                        System.out.println("Triangle calculations:");
                        outArea = myGeometry.getTArea();
                        outVolume = myGeometry.getTVolume();
                        outPerimeter=myGeometry.getTPerimeter();        
                        break;

                case 4:
                        System.out.println("Square/rectangle calculations:");
                        outArea = myGeometry.getShapeArea();
                        outVolume = myGeometry.getVolume();
                        outPerimeter=myGeometry.getPerimeter();
                        double fsArea = geom4Sides.getShapeArea();
                        double fsVolume = geom4Sides.getVolume();
                        //double fsPerimeter=geom4Sides.getPerimeter();
                        System.out.println("Using the fourSides class, Area=" + fsArea + " Volume=" + fsVolume);
                        break;
                default:
                        System.out.println("Invalid argument count = " + acount + " Pass in 3 or 4 arguments");
                }
                System.out.println("Area=" + outArea + " Volume=" + outVolume+ " Perimeter=" + outPerimeter);
        }
}

Now,here is the basicGeometry class:

import java.util.*;
public class basicGeometry{
        double firstDimension=12;
        double secondDimension=20;
        double thirdDimension=4;
        public void setShape(double dim1,double dim2,double dim3){
                firstDimension=dim1;
                secondDimension=dim2;
                thirdDimension=dim3;
        }
}

Now,here is the threeSides class:

import java.util.*;

public class threeSides extends fourSides{
        double inHalf=.5;
        public int setDimensions(int firstDimension,int secondDimension,int threeDimension){
                firstDimension=12;
                secondDimension=20;
                threeDimension=4;
                return 0;
        }
        public double getTArea(){
                return getShapeArea()*inHalf;
        }
        public double getTVolume(){
                return (firstDimension*secondDimension*thirdDimension*inHalf);
        }
        public double getTPerimeter(){
                return (firstDimension+secondDimension+thirdDimension); 
        }
}

Now,here is the fourSides class:

import java.util.*;

public class fourSides extends basicGeometry{
        
        public int setDimensions(int firstDimension,int secondDimension,int threeDimension){
                firstDimension=12;
                secondDimension=20;
                threeDimension=4;
                return 0;
        }
        public double getShapeArea(){
                return (firstDimension*secondDimension);
        }
        public double getVolume(){
                return (firstDimension*secondDimension*thirdDimension);
        }
        public double getPerimeter(){
                return (2*firstDimension*secondDimension);
        }
}

Here is the modification of the program.

Thanks!...


Related Solutions

modify the code below to create a GUI program that accepts a String from the user...
modify the code below to create a GUI program that accepts a String from the user in a TextField and reports whether or not there are repeated characters in it. Thus your program is a client of the class which you created in question 1 above. N.B. most of the modification needs to occur in the constructor and the actionPerformed() methods. So you should spend time working out exactly what these two methods are doing, so that you can make...
Get the file “HW4Part4b.java” from the repository. Modify the program class to match the new file...
Get the file “HW4Part4b.java” from the repository. Modify the program class to match the new file name. Complete it by writing a recursive static int function named recur that is defined as follows: if i ≤ 0 or j ≤ 0, recur(i, j) = 0. if i = j, recur(i, j) = i. if i > j, recur(i, j) = j. In all other cases, recur(i, j) = 2 · recur(i − 1, j) + recur(j − 1, i) Add...
Modify the program 5-13 from page 279 such that will also compute the class average. This...
Modify the program 5-13 from page 279 such that will also compute the class average. This class average is in addition to each individual student score average. To accomplish this additional requirement, you should do the following: 1. Add two more variables of type double: one for accumulating student averages, and one to hold the class average. Don't forget, accumulator variable should be initialized to 0.0. 2. Immediately after computing individual student average, add a statement that will accumulate the...
Modify the linked list code from class to work with strings. Insert the following food items...
Modify the linked list code from class to work with strings. Insert the following food items into the list and display the list. The items are: bread, noodles, milk, bananas, eggs. Insert them in that order. Display the list. Then delete milk and redisplay the list. Then insert ice cream and redisplay the list. Then append zucchini and redisplay the list. c++
Modify the DetailedClockPane.java class in your detailed clock program, to add animation to this class. Be...
Modify the DetailedClockPane.java class in your detailed clock program, to add animation to this class. Be sure to include start() and stop() methods to start and stop the clock, respectively.Then write a program that lets the user control the clock with the start and stop buttons.
5. Modify the program for Line Numbers from L09: In-Class Assignment. Mainly, you are changing the...
5. Modify the program for Line Numbers from L09: In-Class Assignment. Mainly, you are changing the problem from using arrays to ArrayLists. There are some other modifications as well, so read the instructions carefully. The program should do the following: –Ask the user for how many lines of text they wish to enter –Declare and initialize an **ArrayList** of Strings to hold the user’s input –Use a do-while loop to prompt for and read the Strings (lines of text) from...
NOTE - Submission in parts. Parts required - Dog Class Code, Dog Manager Class Code and...
NOTE - Submission in parts. Parts required - Dog Class Code, Dog Manager Class Code and the main code. Please differentiate all three in the answer. This Assignment is designed to take you through the process of creating basic classes, aggregation and manipulating arrays of objects. Scenario: A dog shelter would like a simple system to keep track of all the dogs that pass through the facility. The system must record for each dog: dogId (int) - must be unique...
C++ code Write a program to illustrate how to use the temporary class. Your program must...
C++ code Write a program to illustrate how to use the temporary class. Your program must contain statements that would ask the user to enter data of an object and use the setters to initialize the object. Use three header files named main.cpp, temporary.h, and temporaryImp.cpp An example of the program is shown below: Enter object name (rectangle, circle, sphere, or cylinder: circle Enter object's dimensions: rectangle (length and width) circle (radius and 0) sphere (radius and 0) rectangle (base...
Modify the provided code to create a program that calculates the amount of change given to...
Modify the provided code to create a program that calculates the amount of change given to a customer based on their total. The program prompts the user to enter an item choice, quantity, and payment amount. Use three functions: • bool isValidChoice(char) – Takes the user choice as an argument, and returns true if it is a valid selection. Otherwise it returns false. • float calcTotal(int, float) – Takes the item cost and the quantity as arguments. Calculates the subtotal,...
Your task is to modify the program from the Java Arrays programming assignment to use text...
Your task is to modify the program from the Java Arrays programming assignment to use text files for input and output. I suggest you save acopy of the original before modifying the software. Your modified program should: contain a for loop to read the five test score into the array from a text data file. You will need to create and save a data file for the program to use. It should have one test score on each line of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT