Question

***** Using Java **** Please type out i couldn't understand the code someone wrote out. Implement...

***** Using Java **** Please type out i couldn't understand the code someone wrote out.

Implement a program for computing areas of three types of play-place units: Rectangular, Circular and Triangular. A contractor builds play-places (with materials such as wood, iron, pads, plastics etc.) at customer sites using play place units of different dimensions. The charges are based on the area of each unit plus the number of units. The software system is needed for computing cost which is based on Total Area. The cost is: $10.00 per square foot. A price discount of 10% is available if the total cost is above $1000.00. Users should be able to enter input interactively using a Graphical User Interface (GUI). You can make additional assumptions; please state your significant assumptions.

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

////////////////////////////////////////////////////

package test.playplace;

import java.awt.Button;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Component;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
public class PlayPlaceGui
{
   private PlayPlaceUnitShape shape = null;
   //The Constructor
   public PlayPlaceGui(){
Frame f= new Frame("Play Place cost calculator"); //frame name
f.setSize(600,600);

Label label = new Label("Choose Play-place Unit type");
label.setBounds(20,50,200,20);
label.setAlignment(Label.CENTER);

f.add(label);

CheckboxGroup cbg = new CheckboxGroup();

Checkbox rectCB = new Checkbox("Rectangular", cbg, false);
rectCB.setBounds(50,90,100,50);
Checkbox circCB = new Checkbox("Circular", cbg, false);
circCB.setBounds(170,90,100,50);
Checkbox triCB = new Checkbox("Triangular", cbg, false);
triCB.setBounds(290,90,100,50);

f.add(rectCB);
f.add(circCB);
f.add(triCB);



//The following data components will initially
//not be visible. The will be visible once user selects any one of
//the Rectangular/Circular/Triangular option from above
//depending on how many data those options need

List<Component> dataComponents = new ArrayList<Component>();

Label dataLbl1 = new Label();
dataLbl1.setBounds(50,150,70,30);
f.add(dataLbl1);
dataComponents.add(dataLbl1);

TextField dataTxt1 = new TextField();
dataTxt1.setBounds(140,150,100,30);
f.add(dataTxt1);
dataComponents.add(dataTxt1);

Label dataLbl2 = new Label();
dataLbl2.setBounds(50,190,70,30);
f.add(dataLbl2);
dataComponents.add(dataLbl2);

TextField dataTxt2 = new TextField();
dataTxt2.setBounds(140,190,100,30);
f.add(dataTxt2);
dataComponents.add(dataTxt2);

Label dataLbl3 = new Label();
dataLbl3.setBounds(50,230,70,30);
f.add(dataLbl3);
dataComponents.add(dataLbl3);

TextField dataTxt3 = new TextField();
dataTxt3.setBounds(140,230,100,30);
f.add(dataTxt3);
dataComponents.add(dataTxt3);

//This method cal will make the above data components
//make invisible
setComponentsVisible(dataComponents, false);


//The button will be shown after the above data components
//initially will not be visible
Button btn = new Button("Calculate Cost");
btn.setBounds(50, 270, 100, 25);
btn.setVisible(false);
f.add(btn);



//below are the final label and text field components
//where the result will be shown
//There will be : Total area , Total Cost , Total Discount and Final Cost
List<Component> finalComponents = new ArrayList<Component>();

Label totalAreaLabel = new Label("Total Play-place Area:");
totalAreaLabel.setBounds(60,300,120,30);
f.add(totalAreaLabel);
finalComponents.add(totalAreaLabel);

TextField tfTotalArea = new TextField();
tfTotalArea.setBounds(190,300,80,30);
tfTotalArea.setEnabled(false);
f.add(tfTotalArea);
finalComponents.add(tfTotalArea);

Label totalCostLbl = new Label("Total Cost");
totalCostLbl.setBounds(60,350,120,30);
f.add(totalCostLbl);
finalComponents.add(totalCostLbl);

TextField tfTotalCost = new TextField();
tfTotalCost.setBounds(190,350,80,30);
tfTotalCost.setEnabled(false);
f.add(tfTotalCost);
finalComponents.add(tfTotalCost);

Label totalDiscountLbl = new Label("Total Discount");
totalDiscountLbl.setBounds(60,390,120,30);
f.add(totalDiscountLbl);
finalComponents.add(totalDiscountLbl);

TextField tfTotalDisc = new TextField();
tfTotalDisc.setBounds(190,390,80,30);
tfTotalDisc.setEnabled(false);
f.add(tfTotalDisc);
finalComponents.add(tfTotalDisc);

Label finalTotalLbl = new Label("Final total");
finalTotalLbl.setBounds(60,430,120,30);
f.add(finalTotalLbl);
finalComponents.add(finalTotalLbl);

TextField tfFinalTotal = new TextField();
tfFinalTotal.setBounds(190,430,80,30);
tfFinalTotal.setEnabled(false);
f.add(tfFinalTotal);
finalComponents.add(tfFinalTotal);
  
//this method call will make the above finalComponents make invisible
setComponentsVisible(finalComponents,false);

f.setLayout(null);
f.setVisible(true);


//action listener for the button
//this will be triggered when button is clicked
btn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            double area = 0;
            double val1,val2,val3;
            //based on the type of shape value will be taken from
            //the text field of dataComponent
            //and area will be calculated by calling calculateArea method
            //of corresponding PlayPlaceUnitShape
            if(shape.getNumber() == 1){//rectangle
                val1 = Double.parseDouble(dataTxt1.getText());
                val2 = Double.parseDouble(dataTxt2.getText());
                Rectangle r = (Rectangle)shape;
                r.setLength(val1);
                r.setWidth(val2);
                area = r.calculateArea();
            }else if(shape.getNumber() == 2){//circle
                val1 = Double.parseDouble(dataTxt1.getText());
                Circle c = (Circle)shape;
                c.setRadius(val1);
                area = c.calculateArea();
            }else if(shape.getNumber() ==3){//Triangle
                val1 = Double.parseDouble(dataTxt1.getText());
                val2 = Double.parseDouble(dataTxt2.getText());
                val3 = Double.parseDouble(dataTxt3.getText());
                Triangle t = (Triangle)shape;
                t.setSide1(val1);
                t.setSide2(val2);
                t.setSide3(val3);
                area= t.calculateArea();
            }
           
            DecimalFormat df = new DecimalFormat("#.##");
            double cost = area * 10; //cost will be totalArea * 10
            double discount = 0;
            if(cost > 1000.0){ //if total cost is > 1000 then discount will be 10%
                discount = cost * 0.1;
            }
            double finalCost = cost - discount;//calculate final Cost
           
            //display the result values in the final components
            tfTotalArea.setText(String.valueOf(df.format(area)));
            tfTotalCost.setText(String.valueOf(df.format(cost)));
            tfTotalDisc.setText(String.valueOf(df.format(discount)));
            tfFinalTotal.setText(String.valueOf(df.format(finalCost)));
           
            //make the final Components visible
            setComponentsVisible(finalComponents,true);
        }
    });

//Item listener for REctangular check box option
//Will be triggered when this item is selected
rectCB.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
   shape = new Rectangle();//instantiate Rectangle shape
   setComponentsVisible(dataComponents,false);
dataLbl1.setText("Length:");dataLbl1.setVisible(true);dataTxt1.setText("0");dataTxt1.setVisible(true);
dataLbl2.setText("Width:");dataLbl2.setVisible(true);dataTxt2.setText("0");dataTxt2.setVisible(true);
btn.setVisible(true);
  
}
});

//Item listener for Circular check box option
//Will be triggered when this item is selected
circCB.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
   shape = new Circle(); //Instantiate Circle shape
   setComponentsVisible(dataComponents,false);
dataLbl1.setText("Radius:");dataLbl1.setVisible(true);dataTxt1.setText("0");dataTxt1.setVisible(true);
btn.setVisible(true);
}
});


//Item listener for Triangular check box option
//Will be triggered when this item is selected
triCB.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
   shape = new Triangle(); //instantiate triangle shape
   setComponentsVisible(dataComponents,false);
dataLbl1.setText("Side1:");dataLbl1.setVisible(true);dataTxt1.setText("0");dataTxt1.setVisible(true);
dataLbl2.setText("Side2:");dataLbl2.setVisible(true);dataTxt2.setText("0");dataTxt2.setVisible(true);
dataLbl3.setText("Side3:");dataLbl3.setVisible(true);dataTxt3.setText("0");dataTxt3.setVisible(true);
btn.setVisible(true);
}
});
}
  
   /**
   * This will make all the components in the list not visible
   * @param components
   * @param value
   */
   private void setComponentsVisible(List<Component> components,boolean value){
       for(Component c: components){
           c.setVisible(value);
       }
   }
  
  
   //the main method
   public static void main(String args[]) {

       new PlayPlaceGui();
   }
}

////////////////////////////////////////////////////////////////////////////

package test.playplace;

//The abstract class
public abstract class PlayPlaceUnitShape {
  
   private int number; //this will be the unique number identifying the PlayPlaceUnitShape
  
   //constructor
   public PlayPlaceUnitShape(int number) {
       this.number = number;
   }
  
   //will be implemented in classes extending this abstract class
   public abstract double calculateArea();
  
   public int getNumber(){
       return number;
   }

}

////////////////////////////////////////////////////////////////////////////

package test.playplace;

public class Rectangle extends PlayPlaceUnitShape{
   private double length,width;
  
   //constructor
   public Rectangle(){
       super(1); //number will be 1 for rectangle, parent constructor will be called first
   }
  
  

   public void setLength(double length) {
       this.length = length;
   }

   public void setWidth(double width) {
       this.width = width;
   }

   @Override
   public double calculateArea() {
       return length* width; //area of rectangle
   }

}

////////////////////////////////////////////////////////////////////////

package test.playplace;

public class Triangle extends PlayPlaceUnitShape{
   private double side1,side2,side3;
  
   public Triangle(){
       super(3);//number will be 3 for Triangle, parent constructor will be called first
   }
  
  
  

   public void setSide1(double side1) {
       this.side1 = side1;
   }


   public void setSide2(double side2) {
       this.side2 = side2;
   }


   public void setSide3(double side3) {
       this.side3 = side3;
   }


   @Override
   public double calculateArea() {
       //calculate the area of triangle
       // area = square root of s*(s-a)*(s-b)*(s-c)
       //where s = hallf perimeter of triangle which is (a+b+c)/2
       //ab.b.c are the three sides of triangle
      
       double halfPeri = (side1+side2+side3)/2;
      
       double result = halfPeri*(halfPeri-side1)*(halfPeri-side2)*(halfPeri -side3);
       return Math.sqrt(result);
      
   }

}

///////////////////////////////////////////////////////////////////

package test.playplace;

public class Circle extends PlayPlaceUnitShape{
   private double radius;
  
   public Circle(){
       super(2);//number will be 2 for Triangle, parent constructor will be called first
   }
  
  

   public void setRadius(double radius) {
       this.radius = radius;
   }

   @Override
   public double calculateArea() {
       return Math.PI*radius*radius; //calculate area of circle ; PI* (radius)^2
   }

}

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

OUTPUT

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

Play Place cost calculator - 0 x Choose Play-place Unit type Rectangular Circular Triangular Length: Length: Width: 27 35.69 Width: Calculate Cost Total Play-place Area: 963.63 Total Cost 9636.3 Total Discount 963.63 Final total 8672.67 3672.67

Play Place cost calculator Choose Play-place Unit type Rectangular Circular Triangular Radius: Calculate Cost 254 47 Total Play-place Area Total Cost 2544.69 Total Discount 254.47 Final total 2290 22

Play Place cost calculator - O X Choose Play-place Unit type Rectangular Circular Triangular Side 1: 5 Side2: 6 Side3: 7.59 Calculate Cost Total Play-place Area: 14.98 Total Cost 149.76 Total Discount Final total 149.76

Add a comment
Know the answer?
Add Answer to:
***** Using Java **** Please type out i couldn't understand the code someone wrote out. Implement...
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
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