Question

In: Computer Science

Question 7                                         &nbs

Question 7                                                                                                                  6 Marks

  1. Given the following code, use lambdas and streams to calculate and display the number of marks greater than or equal to 50 (i.e. the total number that have passed).                                                                                                        (1 mark)

public static void main(String[] args) {

    int[] marks = {10, 53, 65, 49, 46, 95, 81, 45, 72, 85};

         //use lambdas and streams to work out how many

         // marks are >= 50 and display the result.

         <part (a) to complete>

      }

      Output should be similar to the following:

    The number of marks greater than or equal to 50 = 6

  1. The following is the class diagram for the Result class used in this question.

Given the starting point for the code below, use lambdas and streams to complete the main method as follows:

  1. Write a Predicate called supplementary that returns true if the mark is in the supplementary range (>=45 and <50) and false if the mark is not in this range.                                                                                              (1 mark)
  2. Make use of the predicate from part (i) to display all the result records (student id and mark) with marks in the supplementary range     
  3. Use lambdas and streams to calculate and display the average mark.

    

In the answer template complete the code for parts (i) to (iii) where you see <to be completed> in the code below:

public class StreamQuestion {

    public static void main(String[] args) {

        Result[] results = {new Result("S123345", 10),

                            new Result("S678901", 53),

                            new Result("S778901", 65),

                            new Result("S878901", 49),

                            new Result("S078901", 81),

                            new Result("S688901", 45),

                            new Result("S698901", 72),

                            new Result("S679901", 46),

                            new Result("S678911", 72),

                            new Result("S678912", 85)};

       

        List<Result> resultList = Arrays.asList(results);       

                                  

       //(i) Write the code for the supplementary predicate

        <Part (i) To be completed>

   // (ii) using the predicate (and lambdas and streams),  

   // display the student id and mark for all results in

   // the supplementary range

       <Part (ii) To be completed>

   // (iii) using lambdas and streams calculate and display

   // the average mark           

       <Part (iii) To be completed>

    }  

The output generated should be similar to the following:

Supplementary List:

<S878901 49>

<S688901 45>

<S679901 46>

Average Mark: 57.80

Solutions

Expert Solution

Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate the question. Thank You So Much.

Main.java

package staticClass;

import java.util.Arrays;

public class Main {
   public static void main(String[] args) {

       int[] marks = {10, 53, 65, 49, 46, 95, 81, 45, 72, 85};
       int num=0;
       //use lambdas and streams to work out how many

       // marks are >= 50 and display the result.
      
       num=(int) Arrays.stream(marks).filter(e->e>50).count();
      
       System.out.println("The number of marks greater than or equal to 50 = "+num);
   }
}

output

2)

StreamQuestion.java

package staticClass;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

class Result{
   private String id;
   private int mark;
   public Result(String id, int mark) {
       super();
       this.id = id;
       this.mark = mark;
   }
   public String getId() {
       return id;
   }
   public void setId(String id) {
       this.id = id;
   }
   public int getMark() {
       return mark;
   }
   public void setMark(int mark) {
       this.mark = mark;
   }
   @Override
   public String toString() {
       return "<" + id + " " + mark + ">";
   }
  
  

}
public class StreamQuestion {

   public static void main(String[] args) {

       Result[] results = {new Result("S123345", 10),

               new Result("S678901", 53),

               new Result("S778901", 65),

               new Result("S878901", 49),

               new Result("S078901", 81),

               new Result("S688901", 45),

               new Result("S698901", 72),

               new Result("S679901", 46),

               new Result("S678911", 72),

               new Result("S678912", 85)};

       List<Result> resultList = Arrays.asList(results);   

       //(i) Write the code for the supplementary predicate
      
       Predicate<Result> lesserthan = i -> (i.getMark()>=45 && i.getMark()<50);
      
       // (ii) using the predicate (and lambdas and streams),

       // display the student id and mark for all results in

       // the supplementary range
      
       System.out.println("Supplementary List:");
       resultList.stream().filter(lesserthan).forEach(p->System.out.println(p));
      
       // (iii) using lambdas and streams calculate and display
       double sum=resultList.stream().mapToInt(i -> i.getMark()).sum();
       double avg = sum/resultList.size();
       System.out.printf("Average Mark: %.2f",avg);
       // the average mark   

   }
}

output


Related Solutions

Question 5                                         &nbs
Question 5                                                                                                                                          Julian and Jenna carry on a partnership business and for the income year ended 30 June, the partnership net income was $38,000, as returned by their accountant. However, included in the deductions was a salary of $12,000 paid to Julian’s wife (who is not a business partner). The Commissioner disallows all but $2,000 of this amount. Required: What authority, if any, does the Commissioner have in disallowing the claim for salary? (2.5 marks) ANSWER: What course of...
Question 1                                         &nbs
Question 1                                                                                                                    2 Marks Briefly explain the referential integrity rule. Provide a suitable situation when the referential integrity constraint is violated. Question 2                                                                                                                    2 Marks This question refers to the Part table as used in this unit lecture slides about the customers to order homeware parts. Using SQL DCL command to write a statement to permit a user with the log in ID of JOHNSON to access the Part table and update its UnitPrice value. Question 3                                                                                                                    2...
Question 3                                         &nbs
Question 3                                                                                     Prints Galore Ltd., a Canadian company, acquired 100% of Sculptures Ltd. for FC 300,000 on January 1, 2014. Prints Galore’s functional currency is the Canadian dollar and Sculpture’s functional currency is the FC. Selected exchange rates are presented below:                              January 1, 2014                       FC1 = $1.6993 CAD                              December 31, 2015                  FC1 = $1.7182 CAD                              December 31, 2016                  FC1 = $1.7233 CAD Assume that the average rate for 2014, 2015, and 2016 is FC 1...
Question 2                                         &nbs
Question 2                                                                                                                  15 Marks The Anderson Group provides counselling services at different suburbs in NSW. It charges clients for (a) direct professional time (at an hourly rate) and (b) support services (at 30% of the direct professional costs billed). The three counsellors in The Anderson Group and their rates per counselling hour are as follows: Counsellor Billing Rate per Hour Rob Mason $320 Meghana Shreshtha 110 Daniel Jacob 50 The Anderson Group has just prepared the May 2019 bills...
Question 1                                         &nbs
Question 1                                                                                      On October 1, 2010, Madison Ltd. acquired all the shares of Dobson Ltd. for $849,600. On that date, Dobson’s statement of financial position showed share capital of $540,000 and retained earnings of $273,600. In addition, at the acquisition date, all of Dobson’s identifiable assets and liabilities had carrying values that equaled their fair values. Madison and Dobson’s financial statements for September 30, 2014 are presented below: Statement of Financial Position As of September 30, 2014                                                                                                 Madison...
Question 2                                         &nbs
Question 2                                                                                      On January 1, 2015, Portia Ltd. issued shares worth $1,120,000 to Storm Ltd. to acquire 80% of Storm’s outstanding shares. On the acquisition date, Storm’s statement of financial position shows share capital of $420,000 and retained earnings of $777,000. At the acquisition date, all of Storm’s identifiable assets and liabilities equaled their fair values with the exception of the following:                     Inventories (fair value exceeded book value by $14,000)           Investments (fair value exceeded book value...
Question 2                                         &nbs
Question 2                                                                                                                   8 marks The following information relates to the business of Paisley’s Guitar School for the month of June 2019: June 1       Purchased equipment for $42,000. Paid $6,000 deposit and agreed to pay the balance in 60 days. 3       Paid salaries of $15,800. 8       Brad Paisley withdrew $21,000 from the business for his personal use. 14      Paid $9,800 for radio commercials incurred. 19      Paid $7,400 to creditors for supplies that had previously been purchased...
Question 6                                         &nbs
Question 6                                                                                                               9 marks You are the accountant for Serry Company which has agreed to act as guarantor on a loan of $2 million obtained by Webster and Sons. Serry Company, as guarantor, is required to pay back the loan if Webster and Sons cannot pay. Required: In light of the AASB Framework, justify how (or if) the guarantee of the loan would be shown in the books of Serry Company.                                                                                                                                           there is no picture...
QUESTION 1                                         &nbs
QUESTION 1                                                                                                                                From the following information of two sole trader enterprises conducting similar businesses, prepare: Ratio calculations in the table below for comparison of profitability, asset efficiency, liquidity and capital structure. Vertical analysis of the Balance Sheets for both enterprises. Round all calculations to 2 decimal places. Balance Sheet data (end-of-year) Hi Enterprises $ Lo Enterprises $ Current assets 24 000 32 000 Motor vehicles (net) 19 000 27 000 Total assets 43 000 59 000 Current liabilities 14...
QUESTION 2                                         &nbs
QUESTION 2                                                                                                                          Albert Holland, manager of Palm Beach Surfing Supplies has provided the following estimated figures for the months of July to October: Estimated cash balance at the end of August is $50 000. Actual sales for July and August and estimated sales for September and October are as follows: July August September October Cash sales 33 000 31 000 42 000 30 000 Credit sales 60 000 80 000 100 000 50 000 Analysis of past records has...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT