Answer to Question #174293 in Algorithms for Kwame Hay

Question #174293

Question 1

A. Write a java program that reads three (3) int variables and performs the below operations:

i. Prints out their product

ii. Prints out the maximum of the numbers

Do not use any inbuilt Java Methods to return the maximum.

Extra marks will be awarded for clarity of code and comments.

[8 marks]

B. Algorithms perform differently on different data sets and as such we may be interested in either their Best-case, Worst-case or Average-case scenarios. Explain why the Worst-case for an insertion sort is with reverse-sorted data. [6 marks]

C. Identify and correct the errors in each of the following sets of code: [6 marks]

i.

while ( c <= 5 )

{

product *= c;

++c;

ii.

if ( gender == 1 )

System.out.println( "Woman" );

else;

System.out.println( "Man" );

iii. What is wrong with the following while statement?

while ( z >= 0 )

sum += z;

D. Explain two (2) programming situations that will require the use of overloaded


1
Expert's answer
2021-03-28T14:22:23-0400

A.

import java.util.Scanner;


public class Main {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

//read variables

System.out.println("Enter a first int variable:");

int a = in.nextInt();

System.out.println("Enter a second int variable:");

int b = in.nextInt();

System.out.println("Enter a third int variable:");

int c = in.nextInt();

//display the product

System.out.println("The product is: " + a * b * c);


int max;

//find the max variable

if (a >= b && a >= c) {

max = a;

} else if (b >= a && b >= c) {

max = b;

} else {

max = c;

}

//display the max

System.out.println("The maximum of the numbers: " + max);

}

}


B. At each iteration, the number of swaps will be equal to the number of the current iteration. In this case, the sum of all swaps will be the maximum for this type of sorting. Because every next element will be smaller than all the elements before it.


C.

i. There is no closing curly brace.

ii. Semicolon after "else".

iii. If z is greater than or equal to 0, then the loop will become infinite.


D. If you want the child method to have different behavior, when you need to implement an abstract method or an interface method.


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS