Guest User

Untitled

a guest
Dec 12th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.61 KB | None | 0 0
  1. //Ashok Chandwaney
  2. //COSC 120 example code
  3.  
  4. import java.util.*;
  5. public class Orange {
  6.    
  7.     //these variables correspond to the middle level of the UML diagram
  8.     //they're the information that every object of this class will store
  9.     boolean isPeeled;
  10.     int percentLeft;
  11.     boolean isOrganic;
  12.    
  13.     //main method - this is where the program starts running from top to bottom
  14.     public static void main(String[] args)
  15.     {
  16.         System.out.println("Hi i'm a program");
  17.        
  18.        
  19.         //make a new orange, calls the constructor
  20.         Orange myOrange = new Orange();
  21.        
  22.         System.out.println("How much of the orange do you want to eat? Between 0 and 100");
  23.         Scanner scanner = new Scanner(System.in);
  24.         int x = scanner.nextInt();
  25.         myOrange.eat(x);
  26.         int whatsLeft = myOrange.getWhatsLeft();
  27.         System.out.println(whatsLeft + " of your orange is left");
  28.        
  29.         //make another new orange, call the constructor with arguments
  30.         //this particular orange is organic, but not peeled and uneaten
  31.         Orange constructorOrange = new Orange(true, 100, false);
  32.     }
  33.    
  34.     //No-arguments, or "default" constructor for an orange.
  35.     //IMPORTANT! In here every variable from the top (percentLeft, isPeeled, and isOrganic)
  36.     //are given values.  In your default constructor, you should assign a value to every variable
  37.     public Orange()
  38.     {
  39.         isPeeled = false;
  40.         percentLeft = 100;
  41.         isOrganic = false;
  42.     }
  43.    
  44.     //The arguments constructor for an orange
  45.     //Takes in one piece of information corresonding to each of the variables at the top
  46.     //Then puts the values from those pieces of information taken in into the corresponding variable at the top
  47.     public Orange(boolean organic, int left, boolean peeled)
  48.     {
  49.         isPeeled = peeled;
  50.         percentLeft = left;
  51.         isOrganic = organic;
  52.     }
  53.    
  54.     //peels the orange
  55.     public void peel()
  56.     {
  57.         isPeeled = true;
  58.     }
  59.    
  60.     //eats a percentage of the orange
  61.     public void eat(int percent)
  62.     {
  63.         System.out.println("You have eaten " + percent + " of your orange");
  64.        
  65.         //subtracts the specified percent from the percentLeft
  66.         percentLeft = percentLeft - percent;
  67.     }//note: at this point the value in percent stops existing
  68.    
  69.     public int getWhatsLeft()
  70.     {
  71.         //the return statement makes the program stop what it's doing and throw back a value
  72.         //so this line makes the program stop and send back the value in percentLeft
  73.         return percentLeft;
  74.     }
  75. }
Add Comment
Please, Sign In to add comment