Question

In: Computer Science

I have questions in regards to the following code 1. package Week_5; 2. import java.util.InputMismatchException; 3....

I have questions in regards to the following code

1. package Week_5;

2. import java.util.InputMismatchException;

3. import java.util.Scanner;

4. public class Customer {

5. public String firstName;

6. public String lastName;

7. public String FullName()

8. {

9. return this.firstName+" "+this.lastName;

10. }

11. }

12. public class ItemsForSale {

13. public String itemName;

14. public double itemCost;

15. public boolean taxable;

16. public void PopulateItem(String iName, double iCost, boolean canTax)

17. {

18. this.itemName = iName;

19. this.itemCost = iCost;

20. this.taxable = canTax;

21. }

22. }

23. class PRG215_Week_5 {

24. public static void main(String[] args) {

25. final int TOTAL_ITEMS = 6;

26. ItemsForSale[] items = new ItemsForSale[TOTAL_ITEMS];

27. for(int i = 0; i < TOTAL_ITEMS; i++)

28. {

29. items[i] = new ItemsForSale();

30. }

31. items[0].PopulateItem("Tennis Shoes",45.89,true);

32. items[1].PopulateItem("Shits", 25.55, true);

33. items[2].PopulateItem("Coats", 89.99, true);

34. items[3].PopulateItem("Belts", 15, true);

35. items[4].PopulateItem("Pants", 25.99, true);

36. items[5].PopulateItem("Donation", 10, false);

37. double totalAmount = 0.0;

38. double totalTax = 0.0;

39. double taxRate = 0.081;

40. final double DISCOUNT_RATE = 0.025;

41. final double AMOUNT_TO_QUALIFY_FOR_DISCOUNT = 100;

42. double discountAmount = 0;

43. System.out.println("The following clothing items are available for purchase:");

44. for(int i=0; i < items.length; i++)

45. {

46. //Display each item in the array

47. System.out.println(" "+(i + 1)+". "+items[i].itemName+ " for $"+items[i].itemCost+" each");

48. }

49. System.out.println("");

50. Scanner keyboard = new Scanner(System.in);

51. Customer newCust = new Customer();

52. System.out.print("Please enter your first name: ");

53. newCust.firstName = keyboard.next();

54. System.out.print("Please enter your last name: ");

55. newCust.lastName = keyboard.next();

56. System.out.println("");

57. System.out.println("Ok, " + newCust.FullName()+", please enter the product ID (the number to the left of the item name) that you wish to purchase. Enter 0 when you are finished.");

58. int itemID = 0;

59. int itemCounter = 1;

60. do 61. {

62. System.out.print("Please enter item ID number "+(itemCounter)+": ");

63. try

64. {

65. itemID = keyboard.nextInt();

66. if(itemID > 0)

67. {

68. totalAmount = totalAmount + items[itemID - 1].itemCost;

69. if(items[itemID - 1].taxable == true)

70. {

71. totalTax = totalTax + (items[itemID - 1].itemCost * taxRate);

72. }

73. itemCounter++;

74. }

75. }

76. catch (ArrayIndexOutOfBoundsException e1)

77. {

78. System.out.println("The item ID you entered is outside the range of possible items. This must be a number between 1 and "+ TOTAL_ITEMS+". Please re-enter your item ID. ");

79. itemID = -1;

80. }

81. catch (InputMismatchException e2)

82. {

83. //Display the error message

84. System.out.println("The item ID you entered does not appear to be an ingeger number. The item ID must be a number between 1 and "+ TOTAL_ITEMS+". Please re-enter your item ID. ");

85. keyboard.nextLine(); 86. itemID = -1;

87. }

88.

89. } while (itemID != 0); //Check if exit condition has been met

90. if(totalAmount >= AMOUNT_TO_QUALIFY_FOR_DISCOUNT)

91. {

92. discountAmount = totalAmount * DISCOUNT_RATE;

93. }

94. else

95. {

96. discountAmount = 0;

97. }

98. System.out.println("");

99. System.out.println("You selected "+itemCounter+" items to purchase.");

100. System.out.println("Your sales total $"+totalAmount);

101. System.out.println("Your discount amount is $"+discountAmount);

102. System.out.println("Your sales tax is $"+totalTax);

103. System.out.println("The total amount due is $"+(totalAmount - discountAmount + totalTax));

104. System.out.println("");

105. }

106. }

Questions

1. List one variable declaration and one constant declared in the code, along with the line number where you found the declaration. Why did the programmer declare it as a variable or constant?

2. What data types are referenced in the code on pages? For each data type, provide the line number of a declaration using that data type as well as the declaration itself. Additionally, list the primitive data types available in Java that are not referenced in the code.

3. Identify the different operators in the code, along with the line number where you found the operator example. Also identify the type of each operator listed as arithmetic, logical, relational, empty, or conditional.

4. Identify the control flow statements in the code. For each control flow statement, give the line number where you found the statement example. Also, list whether that statement is a decision-making, looping, or branching statement.

5. Identify the class definitions, object instantiations, and method calls in the code. Give the line number where you found each.

Below is a hypothetical example

Example: Class definitions

• Patient, line 22

• InsuranceCompany, line 49

• Physician, line 77 Object instantiations

• Alfred Smith, line 89

• Southwest HMO, line 95

• Dr. Smiley Jones, line 98 Method calls

• scheduleFollowUp(“Anne White”, “02/08/19”, “12:45”), line 124

• remitBill(“George Torres”, 33535), line 155

• referPatient(“Bob Allen”), line 189

Solutions

Expert Solution

//--------------- Answers ------------------

1) List one variable declaration and one constant declared in the code, along with the line number where you found the declaration. Why did the programmer declare it as a variable or constant?

Variable declaration     

                14. public double itemCost; ( declared variable of type double to hold item's cost )

constant declaration

                40. final double DISCOUNT_RATE = 0.025; ( constant is declared using final keyword, so its value is fixed which is assigned initially ) DISCOUNT_RATE is declared as constant so that it will be same throughout the program and it cannot be changed accidently.

---------------------------

2) What data types are referenced in the code on pages? For each data type, provide the line number of a declaration using that data type as well as the declaration itself. Additionally, list the primitive data types available in Java that are not referenced in the code.

Double

14. public double itemCost;

16. public void PopulateItem(String iName, double iCost, boolean canTax)

37. double totalAmount = 0.0;

38. double totalTax = 0.0;

39. double taxRate = 0.081;

40. final double DISCOUNT_RATE = 0.025;

41. final double AMOUNT_TO_QUALIFY_FOR_DISCOUNT = 100;

42. double discountAmount = 0;

Int

                27. for(int i = 0; i < TOTAL_ITEMS; i++)

44. for(int i=0; i < items.length; i++)

58. int itemID = 0;

59. int itemCounter = 1;

Boolean

15. public boolean taxable;

16. public void PopulateItem(String iName, double iCost, boolean canTax)

Other primitive data types - ( byte char short long float ) which are not referenced in the code

---------------------------

3) Identify the different operators in the code, along with the line number where you found the operator example. Also identify the type of each operator listed as arithmetic, logical, relational, empty, or conditional.

Assignment operator =

18. this.itemName = iName;

19. this.itemCost = iCost;

20. this.taxable = canTax;

27. for(int i = 0; i < TOTAL_ITEMS; i++) here < is relational operator

27. for(int i = 0; i < TOTAL_ITEMS; i++) here ++ is arithmetic increment operator

66. if(itemID > 0) here > is relational operator

68. totalAmount = totalAmount + items[itemID - 1].itemCost; here + and – are arithmetic operators

71. totalTax = totalTax + (items[itemID - 1].itemCost * taxRate); here * is arithmetic operator

90. if(totalAmount >= AMOUNT_TO_QUALIFY_FOR_DISCOUNT) here >= is relational operator

89. } while (itemID != 0); here != is relational not equal to operator

------------------------------

4. Identify the control flow statements in the code. For each control flow statement, give the line number where you found the statement example. Also, list whether that statement is a decision-making, looping, or branching statement.

Control flow statements (decision-making, looping, or branching statement.)

27. for(int i = 0; i < TOTAL_ITEMS; i++) here for is looping statement

44. for(int i=0; i < items.length; i++) here for is looping statement

60. do here do is looping statement

66. if(itemID > 0) here if is decision making statement

69. if(items[itemID - 1].taxable == true) here if is decision making statement

90. if(totalAmount >= AMOUNT_TO_QUALIFY_FOR_DISCOUNT)

94. else (90, 94 is if else decision making statement )

----------------------------

5) Identify the class definitions, object instantiations, and method calls in the code. Give the line number where you found each.

class definitions --------------------

4. public class Customer {

12. public class ItemsForSale {

23. class PRG215_Week_5 {

object instantiations --------------------

29. items[i] = new ItemsForSale();

50. Scanner keyboard = new Scanner(System.in);

51. Customer newCust = new Customer();

****** 26. ItemsForSale[] items = new ItemsForSale[TOTAL_ITEMS]; this line is for declaring array of objects, objects are not instantiated/created in this line, they are created in line 29

method calls --------------------

31 to 36 lines are PopulateItem( ) method called

31. items[0].PopulateItem("Tennis Shoes",45.89,true);

32. items[1].PopulateItem("Shits", 25.55, true);

33. items[2].PopulateItem("Coats", 89.99, true);

34. items[3].PopulateItem("Belts", 15, true);

35. items[4].PopulateItem("Pants", 25.99, true);

36. items[5].PopulateItem("Donation", 10, false);

49. System.out.println(""); here println method is called

52. System.out.print("Please enter your first name: "); here print method is called

53. newCust.firstName = keyboard.next(); here next() method is called to read word/string from keyboard

55. newCust.lastName = keyboard.next(); here next() method is called to read word/string from keyboard

65. itemID = keyboard.nextInt(); here next() method is called to read integer number from keyboard

85. keyboard.nextLine(); here nextLine() method is called to read complete line of string from keyboard including spaces and tabs, means whole line until enter is pressed

57. System.out.println("Ok, " + newCust.FullName() here FullName() method is called

Thank you.....


Related Solutions

This is my code I need to complete it? //The code package arraylists; import java.util.ArrayList; import...
This is my code I need to complete it? //The code package arraylists; import java.util.ArrayList; import java.util.Scanner; /** * * */ public class SoftOpening {    public static void main(String[] args) {       Scanner input = new Scanner(System.in);    ArrayList foodList = generateMenu();    User user = generateUser(input); user.introduce();    userBuyFood(foodList, user, input); user.introduce(); } public static ArrayList generateMenu(){    ArrayList foodList = new ArrayList<>();    Food pizza1 =new Food(1,"pizza","Seafood",11,12); Food pizza2 =new Food(2,"pizza","Beef",9,10); Food Friedrice =new Food(3,"fried rice","Seafood",5,12);...
Can you fix the errors in this code? package demo; /** * * */ import java.util.Scanner;...
Can you fix the errors in this code? package demo; /** * * */ import java.util.Scanner; public class Booolean0p {        public class BooleanOp {            public static void main(String[] args) {                int a = 0, b = 0 , c = 0;                Scanner kbd = new Scanner(System.in);                System.out.print("Input the first number: ");                a = kbd.nextInt();                System.out.print("Input...
create code for deletestudentbyID Number for choice == 4 package courseecom616; import java.util.Scanner; import java.util.ArrayList; public...
create code for deletestudentbyID Number for choice == 4 package courseecom616; import java.util.Scanner; import java.util.ArrayList; public class CourseeCOM616 {        private String courseName;     private String[] studentsName = new String[1];     private String studentId;        private int numberOfStudents;        public CourseeCOM616(String courseName) {         this.courseName = courseName;     }     public String[] getStudentsName() {         return studentsName;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public String getStudentId() {         return studentId;    ...
INSTRUCTIONS: I HAVE ALREADY ANSWERED QUESTION 1 AND 2. I NEED ASSISTANCE WITH QUESTIONS 3 AND...
INSTRUCTIONS: I HAVE ALREADY ANSWERED QUESTION 1 AND 2. I NEED ASSISTANCE WITH QUESTIONS 3 AND 4. I HAVE FILLED OUT THE PERCENTAGE CHANGE FOR QUESTION 3, AND NEED HELP ON CALCULATING THE OPERATING, INVESTING, AND FINANCIAL SECTIONS. AS WELL AS, THE EQUATIONS FOR QUESTION 4. IF YOU CAN ANSWER QUESTIONS 3 & 4 I WILL AWARD CREDIT. Question 1: Common size for income statement Income Statement (Common Size) :                                                                  Consolidated Income Statement 2011 % 2010 % Revenue $19,176.1...
I got the read-ahead package from the project staff. And I have a few questions. Q#1...
I got the read-ahead package from the project staff. And I have a few questions. Q#1 (100 points)   This table shows some of the info for the week. 20 sample M923’s were produced. It looks like the parts are in some kind of multiple per unit. The first section shows the quantity of each part and the total cost. But doesn’t anybody think that I’d be interested in knowing the price for the part? And I see the direct labor...
I need these written in shell code 1.nested loop. e.g. 1*2 + 2*3 + 3*4 +...
I need these written in shell code 1.nested loop. e.g. 1*2 + 2*3 + 3*4 + ...(n-1)*n. (Only nested loops) 2.Fibonacci numbers.
1. Translate the following code into MIPS code. B[i + 10] = B[i -2] + 40;...
1. Translate the following code into MIPS code. B[i + 10] = B[i -2] + 40; i = i + 10; B[3] = B[i - 1]; a) Assume B is an array of integers (each integer takes 4 bytes). B's address is stored at register $10. Also assume that the compiler associates the variable i to the register $11. b) Assume B is an array of characters (each character takes one byte). B's address is stored at register $10. Also...
I have the following code: //Set.java import java.util.ArrayList; public class Set<T> { //data fields private ArrayList<T>...
I have the following code: //Set.java import java.util.ArrayList; public class Set<T> { //data fields private ArrayList<T> myList; // constructors Set(){ myList = new ArrayList<T>(); } // other methods public void add(T item){ if(!membership(item)) myList.add(item); } public void remove(T item){ if(membership(item)) myList.remove(item); } public Boolean membership(T item){ for (T t : myList) if (t.equals(item)) return true; return false; } public String toString(){ StringBuilder str = new StringBuilder("[ "); for(int i = 0; i < myList.size() - 1; i++) str.append(myList.get(i).toString()).append(", "); if(myList.size()...
Given the following code: for (i=2;i<100;i=i+1) { a[i] = b[i] + a[i]; /* S1 */ c[i-1]...
Given the following code: for (i=2;i<100;i=i+1) { a[i] = b[i] + a[i]; /* S1 */ c[i-1] = a[i] + d[i]; /* S2 */ a[i-1] = 2 * b[i]; /* S3 */ b[i+1] = 2 * b[i]; /* S4 */ } a. List all the dependencies by their types (TD: true-data, AD: anti-data, OD: output-data dependencies). b. Show how Software Pipelining can exploit parallelism in this code to its fullest potential. How many functional units would be sufficient to achieve the...
/* * Assignment: #3 * Topic: Identifying Triangles * Author: <YOUR NAME> */ package edu.depaul.triangle; import...
/* * Assignment: #3 * Topic: Identifying Triangles * Author: <YOUR NAME> */ package edu.depaul.triangle; import static edu.depaul.triangle.TriangleType.EQUILATERAL; import static edu.depaul.triangle.TriangleType.ISOSCELES; import static edu.depaul.triangle.TriangleType.SCALENE; import java.util.Scanner; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * A class to classify a set of side lengths as one of the 3 types * of triangle: equilateral, isosceles, or scalene. * * You should not be able to create an invalid Triangle. The * constructor throws an IllegalArgumentException if the input * cannot be interpreted as...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT