Question

EX 1) Determine the outcome of the following code fragment without writing and running the program....

EX 1) Determine the outcome of the following code fragment without writing and running the program.

int num = 0, max = 20;

while (num <= max)

{

    System.out.println(num);

    num += 4;

}

EX 2) Write a code fragment that reads and prints integer values entered by user until a particular sentinel value (stored in constant SENTINEL) is entered. Do not print the sentinel value.

     

         

Ex 3)  Write a method called averageLargestTwo  that accepts three int parameters  and returns the average of the two largest numbers. For example, 1,5,9 method returns result of 7.  For numbers 5,8,5 method returns 6.5.

public double averageLargestTwo ( int n1, int n2, int n3 )

{

   

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

EX 1) Output of the fragment is:

0
4
8
12
16
20

Ex 2)

final int SENTINEL = 7;
while(true)
        {
                System.out.print("Enter a value: "); //input prompt
                int val = sc.nextInt(); //take input from user
                if(val == SENTINEL) //check if value is equal to the sentinel
                {
                        break; //break the loop
                }
                System.out.println(val); //else print the value
        }

Output:

Enter a value: 1 1 Enter a value: 2 2 Enter a value: 3 3 Enter a value: 4 4 Enter a value: 5 5 Enter a value: 6 6 Enter a val

EX3) Write a method called averageLargestTwo  that accepts three int parameters  and returns the average of the two largest numbers. For example, 1,5,9 method returns result of 7.  For numbers 5,8,5 method returns 6.5.

public static double averageLargestTwo(int n1, int n2, int n3)
        {
                int largest = 0; //set largest as 0
                int slargest = 0; //set second largest as 0
                if(n1 > n2 && n1 > n3) //if n1 is greater than both n2 and n3
                {
                        largest = n1;//n1 is the largest 
                        if(n2>n3) //if n2>n3, n2 is second largest 
                        {
                                slargest = n2;
                        }
                        else
                        {
                                slargest = n3; //else n3 is second largest 
                        }
                }
                else if(n2 > n1 && n2 > n3) //if n2 is greater than both n1 and n3
                {
                        largest = n2; //n2 is largest 
                        if(n1 > n3) //if n1 is larger than n3
                        {
                                slargest = n1; //n1 is 2nd largest 
                        }
                        else
                        {
                                slargest = n3; //else n3 is 2nd largest 
                        }
                }
                else
                {
                        largest = n3; //else n3 is largest 
                        if(n1>n2) //if n1>n2
                        {
                                slargest = n1; //second largest is n1 if it is greater than n2
                        }
                        else
                        {
                                slargest = n2; //else second largest is n2 if greater than n1
                        }
                }
                return (double)(largest + slargest)/2; //return average of largest two.
        }

System.out.println(averageLargestTwo(5, 9, 8));

gives output : 8.5 (if you want to call the function without creating an object, please declare it as static)

Add a comment
Know the answer?
Add Answer to:
EX 1) Determine the outcome of the following code fragment without writing and running the program....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #1. Operators and values, expressions Please correct the following code to make it print the given...

    #1. Operators and values, expressions Please correct the following code to make it print the given expressions and their answers well (3pts). public class DataTypes {    public static void main(String[] args) {       System.out.println("5*8/6 = " + 5*8/6);       System.out.println("(2*6)+(4*4)+10 = " + (2*6)+(4*4)+10);       System.out.println("6 / 2 + 7 / 3 = " + 6 / 2 + 7 / 3);       System.out.println("6 * 7 % 4 = " + 6 * 7 % 4 );       System.out.println("22 + 4 * 2 = "...

  • Hello, Could you please input validate this code so that the code prints an error message...

    Hello, Could you please input validate this code so that the code prints an error message if the user enters in floating point numbers or characters or ANYTHING but VALID ints? And then could you please post a picture of the output testing it to make sure it works? * Write a method called evenNumbers that accepts a Scanner * reading input from a file with a series of integers, and * report various statistics about the integers to the...

  • Please show screenshot outputs and fully functional code for the Java programs. Question 1: Write a...

    Please show screenshot outputs and fully functional code for the Java programs. Question 1: Write a method that computes and returns the area of a square using the following header: public static double area ( double side) Write a main method that prompts the user to enter the side of a square, calls the area method then displays the area. Question 1a: Write a method that converts miles to kilometers using the following header: public static double convertToKilometers (double miles)...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • Please help. I need a very simple code. For a CS 1 class. Write a program...

    Please help. I need a very simple code. For a CS 1 class. Write a program in Java and run it in BlueJ according to the following specifications: The program reads a text file with student records (first name, last name and grade). Then it prompts the user to enter a command, executes the command and loops. The commands are the following: "printall" - prints all student records (first name, last name, grade). "firstname name" - prints all students with...

  • Could someone re-write this code so that it first prompts the user to choose an option...

    Could someone re-write this code so that it first prompts the user to choose an option from the calculator (and catches if they enter a string), then prompts user to enter the values, and then shows the answer. Also, could the method for the division be rewritten to catch if the denominator is zero. I have the bulk of the code. I am just having trouble rearranging things. ------ import java.util.*; abstract class CalculatorNumVals { int num1,num2; CalculatorNumVals(int value1,int value2)...

  • use java and write in text a. Rewrite the following code segment using if statements. Assume...

    use java and write in text a. Rewrite the following code segment using if statements. Assume that grade has been declared as of type char. char grade= 'B';; switch (grade) { case 'A': System.out.println("Excellent"); break case 'B': System.out.println("Good"); default: System.out.println("you can do better”); } - b. write Java code that inputs an integer and prints each of its digit followed by ** e.gif the integer is 1234 then it should print 1**2**3**4 e.g. if the integer is 85 then it...

  • 5. (12 points) Consider the following code fragment (part of a program): int index 3; String...

    5. (12 points) Consider the following code fragment (part of a program): int index 3; String dna "ACTGTCA char nucleotide dna.charAt (index); Matching: For each term below, write the letter of the ONE choice that best matches the term. Data types Variables Literals Object Method ーParameter nde x a. coumt, nucleotide, and d b. 3 and "ACTGTCA c. int, char, and String d. index e. charAt f. dna na 6. (10 points) a. Wrte a statement that assigns true to...

  • DO NOT USE ANY EXTERNAL LIBRARIES BESIDES BUILT IN JAVA LIBRARIES! KEEP IT SIMPLE!!! Provided Code:...

    DO NOT USE ANY EXTERNAL LIBRARIES BESIDES BUILT IN JAVA LIBRARIES! KEEP IT SIMPLE!!! Provided Code: import java.util.Scanner; public class OddAndEven{ /* PART 1: Create a nonstatic method that takes in an int number quantity (n) and returns a returns a String of numbers from 0 to n (inclusive) as the example above demonstrates. Call this quantityToString.    In this method you should check that n is between 0(inclusive) and 100(inclusive). If n is outside these boundaries return and empty...

  • I need to create a code for this prompt: In this project we will build a...

    I need to create a code for this prompt: In this project we will build a generic UserInput class for getting keyboard input from the user. Implementation: The class UserInput is a 'Methods only' class, and all the methods should be declared static. Look at the TestScanner.java program at the bottom of this page that inputs a string, int and double. It shows you how to use Scanner class to get input from the keyboard. Write FOUR simple methods, one...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT