Question: (JAVA) Please only give one answer to each question! 1) A programmer wrote the following code but it always assigns 0 to the total variable.

(JAVA)

Please only give one answer to each question!

1)

A programmer wrote the following code but it always assigns 0 to the total variable. How would you fix it? Rewrite ONLY the last statement.

int employeeDiscount; employeeDiscount = 15; double price; price = 99.99; double total; total = price * ((100-employeeDiscount)/100); // write the statement that will replace the statement above

2)

In this country, we say that someone is a teenager if their age is 13, 14, 15, 16, 17, 18 or 19 years. Write an if/else statement that prints "you are a teenager" or "you are not a teenager" depending on the user's age. In order to earn full points, your code must be efficient. I have started the program for you:

Scanner scanner = new Scanner(System.in); System.out.println("What is your age?"); int age; age = scanner.nextInt(); // your if/else statement will be added right here

3)

Write a for loop that sums up all the numbers in an array and prints the total. I have started the program by allocating the array and filling it with random numbers, so you only have to write the code that sums up all the numbers in the array and prints the total.

Random random; random = new Random(); double[ ] arrayOfDouble; final int NUMBER_OF_ELEMENTS = 7; arrayOfDouble = new double[NUMBER_OF_ELEMENTS]; for (int i=0; i < arrayOfDouble.length; i++ ) { arrayOfDouble [i] = random.nextInt(); } // your code will be added right here

4)

Write the definition for a method called "digitName()" that returns a String containing the name of the digit sent in as parameter. You can assume that your method will always receive an integer that is between 0 and 9, inclusive. ("Inclusive" means including 0 and 9.) This method performs no user input and prints no output.

5)

Write a program that calls the digitName() method that you defined in the previous question. Your program will ask the user to type a single digit, read the user's response into an int variable, and then print the name of the user's digit. You can assume that the user will follow instructions.

6)

Read the documentation for the predefined class Point, in the java.awt:

https://docs.oracle.com/javase/7/docs/api/index.html (Links to an external site.)Links to an external site.

Define an ArrayList of Point objects. Create three new Point objects with the following (x, y) values: (0,6), (4,10), (7,1). Add these three new Point objects to the ArrayList. After all three Point objects are in the ArrayList, print the ArrayList to show that the Points are in the list.

7)

Write the definition of a new class Date. One object of class Date represent the month, day and year for one date on the calendar. I have given you the main( ), and you must provide the class definition that will run the given main( ) and print the given output. You only need to define the methods required by this main( ) :

public static void main(String[] args) { Date today; today = new Date( ); today.setMonth(6); today.setDay (25); today.setYear(2018); System.out.println(today); }

8)

Assume that you are given a price in a String variable, and you need to take a discount off of that price in order to calculate the total. Recall that you cannot perform numeric calculations on String values. You will need to convert the String value into a double value in order to perform arithmetic on it. Luckily, there is a method in the Java API that converts a String value into a double value. This method is called Double.parseDouble(), and it is defined inside class Double. You do not have to import anything to call this method since class Double is inside the java.lang package.

In the following program, add the code that calls the method Double.parseDouble() to convert the String priceInString into the double priceInDouble.

final double DISCOUNT = .15; String priceInString; priceInString = "9.99"; double priceInDouble; priceInDouble = // this is where your code will go double total; total = priceInDouble - (priceInDouble * DISCOUNT); System.out.println("The total including discount is: " + total);

Please type just the code that will go on the right hand side of the assignment operator, where you see the comment "// this is where your code will go"

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!