Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Ashok Chandwaney
- //COSC 120 example code
- import java.util.*;
- public class Orange {
- //these variables correspond to the middle level of the UML diagram
- //they're the information that every object of this class will store
- boolean isPeeled;
- int percentLeft;
- boolean isOrganic;
- //main method - this is where the program starts running from top to bottom
- public static void main(String[] args)
- {
- System.out.println("Hi i'm a program");
- //make a new orange, calls the constructor
- Orange myOrange = new Orange();
- System.out.println("How much of the orange do you want to eat? Between 0 and 100");
- Scanner scanner = new Scanner(System.in);
- int x = scanner.nextInt();
- myOrange.eat(x);
- int whatsLeft = myOrange.getWhatsLeft();
- System.out.println(whatsLeft + " of your orange is left");
- //make another new orange, call the constructor with arguments
- //this particular orange is organic, but not peeled and uneaten
- Orange constructorOrange = new Orange(true, 100, false);
- }
- //No-arguments, or "default" constructor for an orange.
- //IMPORTANT! In here every variable from the top (percentLeft, isPeeled, and isOrganic)
- //are given values. In your default constructor, you should assign a value to every variable
- public Orange()
- {
- isPeeled = false;
- percentLeft = 100;
- isOrganic = false;
- }
- //The arguments constructor for an orange
- //Takes in one piece of information corresonding to each of the variables at the top
- //Then puts the values from those pieces of information taken in into the corresponding variable at the top
- public Orange(boolean organic, int left, boolean peeled)
- {
- isPeeled = peeled;
- percentLeft = left;
- isOrganic = organic;
- }
- //peels the orange
- public void peel()
- {
- isPeeled = true;
- }
- //eats a percentage of the orange
- public void eat(int percent)
- {
- System.out.println("You have eaten " + percent + " of your orange");
- //subtracts the specified percent from the percentLeft
- percentLeft = percentLeft - percent;
- }//note: at this point the value in percent stops existing
- public int getWhatsLeft()
- {
- //the return statement makes the program stop what it's doing and throw back a value
- //so this line makes the program stop and send back the value in percentLeft
- return percentLeft;
- }
- }
Add Comment
Please, Sign In to add comment