Question: ANSWER ALL QUESTIONS 1. a. What is the primary difference between a while loop and a do-while loop? Use an example of each to highlight
ANSWER ALL QUESTIONS 1. a. What is the primary difference between a while loop and a do-while loop? Use an example of each to highlight the difference. [3 marks] b. Write a method called leapYears which will take as parameters two years (as integers) and print out the leap years between them (inclusive). The method heading is: public void leapYears(int year1, int year2). Remember that a leap year is one which is divisible by 4. [HINT: utilize a loop and the mod (%) operator] [5 marks] c. Briefly describe an infinite loop and give an example of one.
[2 marks] 2. Consider the following class for items in a store, in which Item objects store an item number, the name of the item and the amount of item in stock. public class Item{ int itemNumber; String itemName; int stock; public Item(int itemNumber, String itemName, int stock){ this.itemNumber = itemNumber; this.itemName = itemName; this.stock = stock; } } a. i. Write a method called isInStock which checks if the item is in stock and returns a Boolean value. [2 marks] ii. Write a method called sellAmount which takes as a parameter the amount of the item being sold and updates the amount in stock, providing the amount in stock will not go below zero. [5 marks] iii. Write a toString() method for the class which will return as a string the item number, the item name and the amount in stock as a string separated by tabs. [3 marks]
b. Consider the following Cashier class: public class Cashier{ Item[] items = new Item[50]; public static void main(String][] args){ Scanner scan = new Scanner(System.in); } //code for parts i and ii here }
i. Write a user controlled loop in the main method above which will allow the user to continually add Item objects to the items array until the user either decides not to or the array is filled. The user should be prompted to enter each of the relevant information for the Item objects. [6 marks] ii. Write a method searchByItemNumber which will take as a parameter the item number of an item being searched for and returns true if such an item is in the array items, and false otherwise. [Hint: Remember to avoid a NullPointerException]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
