Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

IN JAVA PART A PART B 16 /** 2 A class that simulates a moth flying. */ public class Moth public private double position; 7

IN JAVA

PART A

image text in transcribed

PART B

image text in transcribedimage text in transcribed

16 /** 2 A class that simulates a moth flying. */ public class Moth public private double position; 7 /** 9 Initializes the moth given a position. 10 @param initialPosition the initial position 11 */ 12 public Moth(double initialPosition) 13 { 14 -Start below here. To do: approximate lines of code = 1 15 // set the position to the given parameter initialPosition 16 17 -End here. Please do not remove this comment. Reminder: no changes outside the todo regions. 18 } 19 20 /** 21 Moves the moth halfway to the lightPosition. 22 */ 23 public void moveToLight (double lightposition) 24 25 11 -Start below here. To do: approximate lines of code = 1 26 1/ update the current position to be halfway to (i.e. closer so position should be getting smaller) the light Position 27 28 17 -End here. Please do not remove this comment. Reminder: no changes outside the todo regions. 29 } 30 316 /** 32 Reports the current position of the moth. 33 @return the current position 34 */ 35 public double getPosition() 36 37 return position; 38 } 39 40 public static void main(String[] args) 41 42 Moth myMoth = new Moth(10); 43 myMoth.moveToLight (20); System.out.println("Expected position 15.0; Current position + myMoth.getPosition(); 45 myMoth.moveToLight(0); 46 System.out.println("Expected position 7.5; Current position" + myMoth.getPosition()); 47 myMoth.moveToLight(0); 48 System.out.println("Expected position 3.75; Current position" + myMoth.getPosition()); 49 myMoth.moveToLight(0); 50 System.out.println("Expected position 1.875: Current position" + myMoth.getPosition()); 51 } 52 } 44 1 import java.util.ArrayList; 3e /ok 4 A class that keeps track of a dynamic menu. */ 6 public class Menu 7 { private ArrayList menuItems; Constructs a menu with no options. */ public Menu() { -Start below here. To do: approximate lines of code = 1 // initialize menuItems to an empty array list of Strings 1/ -End here. Please do not remove this comment. Reminder: no changes outside the todo regions. } 10 110 12 13 14 15 16 17 18 19 20 21 220 23 24 25 26 27 28 29 30 31 32 33 /** Adds a new menu item to the end of this menu. @param item the menu item to add */ public void addOption(String item) { -Start below here. To do: approximate lines of code = 1 Ti simply add --End here. Please do not remove this comment. Reminder: no changes outside the todo regions. } 34 35 36 37 38 39 40 41 42 43 44 /** Displays the menu, with options numbered starting with 1. (i.e. not o) @return the number that the user supplied */ public void print() -Start below here. To do: approximate lines of code = 3 // for each item in the menu, print the menu item number (add 1 to the item number index in the array list) // followed by a "." followed by the item name. -End here. Please do not remove this comment. Reminder: no changes outside the todo regions. 46 47 48 49 50 51 52 53 54 } public public String getOption(int itemNum) { 11- -----Start below here. To do: approximate lines of code = 3 // given an item number (indexed from 1) if it is a valid item number, // return the item in the menu corresponding to this item number. // If the item number is not valid, return a " 56 57 58 59 60 -End here. Please do not remove this comment. Reminder: no changes outside the todo regions. 62 63 64 } } public class MenuTester 40 public static void main(String[] args) Menu menu = new Menu(); menu. addOption ("Lasagna"); menu.addOption("Pizza Diavolo"); menu.addOption("Pizza Magherita"); menu.addOption("Pizza Sicilian"); menu.addOption("Chicken Parmigiana"); menu.addoption("Chicken Linguine"); menu.print(); System.out.println("Expected: 1. Lasagna 2. Pizza Diavolo 3. Pizza Magherita 4. Pizza Sicilian 5. Chicken Parmigiana 6. Chicken Linguine"); 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 } 27 System.out.println(menu.getOption(2)); System.out.println("Expected: Pizza Diavolo"); System.out.println(menu.getOption()); System.out.println("Expected: "); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Structured Search For Big Data From Keywords To Key-objects

Authors: Mikhail Gilula

1st Edition

012804652X, 9780128046524

More Books

Students also viewed these Databases questions