Starting from:

$30

CSIT121-Java Lab 8 Solved

Question 1
 

Write a Java application that declares an array of 20 elements to store the prices of items sold at a grocery store. Initialize the array with 20 prices. Then, using lambda and streams perform the following operations on the array created:

 

a)     Display all prices sorted in ascending order

b)     Display the total prices.

c)     Display the most expensive price, the cheapest price, and the average price.

d)     Count and display all prices that are above RM100 sorted in ascending order

e)     Increase all prices to 10% of its original price, sort, and display the new prices.

f)      Display all prices that are in the range of RM100 and RM500 inclusive.

 

 

Question 2
 

Declare and implement a class called Invoice with the following fields:

 

-      part number (an integer value)

-      description (such as hammer, wrench, sugar, flour, etc)

-      type (such as hardware, grocery, etc)

-      quantity (an integer)

-      price (a double value)

 

Implement the following methods for the class Invoice:

 

-      a non-default constructor that takes  parameters to set all of its fields

-      set methods for each of the fields

-      get methods to retrieve each of the fields

 

Use the class Invoice above to create an array of Invoice objects. Use the data given in the data.txt file to set the values for each of the objects in the array. Then, perform the following queries on the array of Invoice objects and display the result.

 

 

 

a)      Use lambdas and streams to sort the Invoice objects by description, then display the results. 

b)     Use lambdas and streams to sort the Invoice objects by price, then display the results. 

c)      Use lambdas and streams to display the Invoice objects group by type. 

d)     Use lambdas and streams to map each Invoice to its description and the value of the invoice (quantity * price). Order the results by Invoice value. 

e)      Re-write (d) to select the Invoice values in the range of RM200 to RM500. 

 

 

Question 3
 

Write a recursive method called power(), that takes as parameters two integers x and y such that x is non-zero and returns xy.  Use the following recursive definition to calculate xy.

 

If y ≥ 0:

                                     1                           if y = 0

         xy  =                x                            if y = 1

                             x  ×  xy-1                if y ≠ 0

 

If y < 0: xy  =               1       .     

                                xy

 

Write a main method to test your recursive method above.  Let the user enters the value for x and y.  Call the recursive method and display the result.

More products