Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LegoHat {
- String style;
- int size;
- public LegoHat (String s, int t){
- this.style = s;
- this.size = t;
- }
- public String toString() {
- return( this.size + " " + this.style );
- }
- public int computeStyle(String season, String style){
- // 3 styles = toque, cap, or visor
- // two season = summer or winter
- int styleScore = 0;
- // toque
- if (season == "summer" && style == "toque") {
- styleScore = 10;
- }
- if (season == "summer" && style == "toque" ){
- styleScore = 1;
- }
- // baseball cap
- if (season == "winter" && style == "cap" ){
- styleScore = 3;
- }
- if (season == "summer" && style == "cap" ){
- styleScore = 7;
- }
- // sun visor
- if (season == "winter" && style == "visor" ){
- styleScore = 1;
- }
- if (season == "summer" && style == "visor" ){
- styleScore = 10;
- }
- return styleScore;
- }
- }
- public class LegoItem {
- String name;
- float weight;
- public LegoItem(String n, float w) {
- this.name = n;
- this.weight = w;
- }
- public String toString(){
- return("a " + this.weight + "-gram " + this.name);
- }
- public boolean isHeavy(float threshold){
- if (this.weight > threshold){
- return true;
- } else {
- return false;
- }
- }
- }
- public class LegoMinifigure {
- String name;
- LegoHat hat;
- LegoItem itemLeft;
- LegoItem itemRight;
- public LegoMinifigure(String n, LegoHat h, LegoItem il, LegoItem ir) {
- this.name = n;
- this.hat = h;
- this.itemLeft = il;
- this.itemRight = ir;
- }
- public String toString(){
- return("A lego minifigure name " + this.name + ", who is wearing a size " + hat.toString() + " and is holding " + itemLeft.toString() + " in its left hand and " + itemRight.toString() + " in its right hand.");
- }
- public void swapHands() {
- LegoItem temp = itemRight;
- itemRight = itemLeft;
- itemLeft = temp;
- }
- public void wearHat(LegoHat hat) {
- this.hat = hat;
- }
- public void placeInLeftHand(LegoItem item){
- this.itemLeft = item;
- }
- public void placeInRightHand(LegoItem item){
- this.itemRight = item;
- }
- public boolean isGood (String season, float threshold){
- boolean reallyIsGood;
- if (hat.computeStyle(season, hat.style) > 6 && itemLeft.isHeavy(threshold) == false && itemRight.isHeavy(threshold) == false){
- reallyIsGood = true;
- } else {
- reallyIsGood = false;
- }
- return reallyIsGood;
- }
- }
- public class TestClass {
- public static void main (String args[]) {
- /***************************/
- /* LegoHat */
- /***************************/
- // creating two hats
- LegoHat hat1 = new LegoHat("toque", 2);
- LegoHat hat2 = new LegoHat("visor", 3);
- // to string test
- String test = hat1.toString();
- System.out.println(test);
- // computer style test
- int result = hat1.computeStyle("summer", hat1.style);
- System.out.println(result);
- /***************************/
- /* LegoItem */
- /***************************/
- // creating four items
- LegoItem item1 = new LegoItem("sword", 10);
- LegoItem item2 = new LegoItem("shield", 15);
- LegoItem item3 = new LegoItem("battle axe", 20);
- LegoItem item4 = new LegoItem("Lance", 7);
- // testing toString
- String test2 = item1.toString();
- System.out.println(test2);
- // esting isHeavy
- boolean test3 = item1.isHeavy(15);
- System.out.println(test3);
- /***************************/
- /* LegoMinifigure */
- /***************************/
- // creating the minifure
- LegoMinifigure figure1 = new LegoMinifigure("figure1", hat1, item1, item2);
- // testing toString
- String test4 = figure1.toString();
- System.out.println(test4);
- // testing SwapHands
- figure1.swapHands();
- String test6 = figure1.toString();
- System.out.println(test6);
- // testing wearHat
- figure1.wearHat(hat2);
- String test7 = figure1.toString();
- System.out.println(test7);
- // testing placeInHand
- figure1.placeInLeftHand(item3);
- String test8 = figure1.toString();
- System.out.println(test8);
- figure1.placeInRightHand(item4);
- String test9 = figure1.toString();
- System.out.println(test9);
- // testing isGood
- boolean test10 = figure1.isGood("summer", 15);
- System.out.println(test10);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement