Advertisement
laiam

David Classes

Feb 9th, 2012
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.66 KB | None | 0 0
  1. /*
  2.  * Name:        David
  3.  * Class:       CS225
  4.  * Program:    
  5.  * Problem:    
  6.  * Input:      
  7.  * Output:      
  8.  */
  9. package classes_3;
  10.  
  11. import java.util.Scanner;
  12.  
  13. /**
  14.  *
  15.  * @author dcrs
  16.  */
  17. public class Main
  18. {//Open Main
  19.  
  20.    public static void main( String[] args )
  21.    {//Open main
  22.       Introduction();
  23.  
  24.       /* ---declarations----------------------------------------------------- */
  25.       //have to pre-initialize these because the switch and loop hides them from
  26.       //the Java Virtual Machine compiler.
  27.       Scanner i = new Scanner(System.in);
  28.       Room Kitchen = new Room(), Bedroom = new Room(), Den = new Room();
  29.       double totalArea;
  30.       String[] rooms = { "Kitchen", "Bedroom", "Den" };
  31.      
  32.       /* ---initializations-------------------------------------------------- */
  33.       /* ---user-input-maybe------------------------------------------------- */
  34.       /* ---calculation-manipulation-engine---------------------------------- */
  35.       for (int count = 0; count < 3; count++) {
  36.          System.out.println("Enter length and width of "+rooms[count]+": ");
  37.          switch(count) {
  38.             case 0:
  39.                Kitchen.setName(rooms[count]);
  40.                Kitchen.setLength(i.nextDouble());
  41.                Kitchen.setWidth(i.nextDouble());
  42.                break;
  43.             case 1:
  44.                Bedroom.setName(rooms[count]);
  45.                Bedroom.setLength(i.nextDouble());
  46.                Bedroom.setWidth(i.nextDouble());
  47.                break;
  48.             case 2:
  49.                Den.setName(rooms[count]);
  50.                Den.setLength(i.nextDouble());
  51.                Den.setWidth(i.nextDouble());
  52.                break;
  53.          }
  54.       }
  55.       totalArea = Kitchen.getArea() + Bedroom.getArea() + Den.getArea();
  56.      
  57.       /* ---output----------------------------------------------------------- */
  58.       System.out.println(Kitchen.printArea());
  59.       System.out.println(Bedroom.printArea());
  60.       System.out.println(Den.printArea());
  61.       System.out.println("Total Area: "+totalArea);
  62.      
  63.       Conclusion();
  64.    }//close main
  65.  
  66.    /* ----------------------------------------------------------------------- *
  67.     * -------------Concluding-Remarks---------------------------------------- *
  68.     * ----------------------------------------------------------------------- */
  69.    public static void Conclusion()
  70.    {//open Conclusion
  71.    }//close Conclusion
  72.  
  73.    /* ----------------------------------------------------------------------- *
  74.     * -------------Introductory---------------------------------------------- *
  75.     * ----------------------------------------------------------------------- */
  76.    public static void Introduction()
  77.    {//open Introduction
  78.    }//close Introduction
  79. }//close Main
  80.  
  81. public class Room
  82. {
  83.    private String name;
  84.    private double length;
  85.    private double width;
  86.    private double area;
  87.  
  88.    /*-------------------------------------------------------------------------*\
  89.    |*---Constructor:-Generate-Initial-Values----------------------------------*|
  90.    \*-------------------------------------------------------------------------*/
  91.    public Room()
  92.    {
  93.       this.name = "Generic Room";
  94.       this.length = 1;
  95.       this.width = 1;
  96.       this.area = length * width;
  97.    }
  98.    
  99.    public Room( String name, double length, double width )
  100.    {
  101.       this.name = name;
  102.       this.length = length;
  103.       this.width = width;
  104.       this.area = length * width;
  105.    }
  106.  
  107.    //-------------------------------------------------------------------------*\
  108.    //---Output----------------------------------------------------------------*|
  109.    //-------------------------------------------------------------------------*/
  110.    public String printArea()
  111.    {
  112.       return "Area of "+this.name+": "+this.area;
  113.    }
  114.  
  115.    public void calculatetArea()
  116.    {
  117.       this.area = length*width;
  118.    }
  119.    
  120.    /*-------------------------------------------------------------------------*\
  121.    |*---Getters-and-Setters---------------------------------------------------*|
  122.    \*-------------------------------------------------------------------------*/
  123.  
  124.    public double getArea()
  125.    {
  126.       return area;
  127.    }
  128.    
  129.    public double getLength()
  130.    {
  131.       return length;
  132.    }
  133.  
  134.    public double getWidth()
  135.    {
  136.       return width;
  137.    }
  138.  
  139.    public String getName()
  140.    {
  141.       return name;
  142.    }
  143.  
  144.    public void setLength( double length )
  145.    {
  146.       this.length = length;
  147.       this.calculatetArea();
  148.    }
  149.  
  150.    public void setWidth( double width )
  151.    {
  152.       this.width = width;
  153.       this.calculatetArea();
  154.    }
  155.  
  156.    public void setName( String name )
  157.    {
  158.       this.name = name;
  159.    }
  160.    
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement