Question

In: Computer Science

TASK: Based upon the following code: import java.util.Scanner; // Import the Scanner class public class Main...

TASK:

Based upon the following code:

import java.util.Scanner; // Import the Scanner class

public class Main {

  public static void main( String[] args ) {

    Scanner myInput = new Scanner(System.in); // Create a Scanner object

    System.out.println("Enter (3) digits: ");

    int W = myInput.nextInt();

    int X = myInput.nextInt();

    int Y = myInput.nextInt();

  

  }

}

Use the tools described thus far to create additional code that will sort the integers in either monotonic ascending or descending order. Copy your code and output below:

import java.util.Scanner; // Import the Scanner class

public class Main {

  public static void main( String[] args ) {

    // Create a Scanner object

    Scanner myInput = new Scanner(System.in);

    // Accept the user's input

    System.out.println("Enter (3) digits: ");

    int W = myInput.nextInt();

    int X = myInput.nextInt();

    int Y = myInput.nextInt();

    // <TODO> - Sort the integers

    int first = 0;

    int second = 0;

    int third = 0;

    // Report the sorted numbers

    System.out.println( "The sorted values are:" );

    System.out.println( first + " " + second + " " + third);

  }

}

Solutions

Expert Solution

I WROTE THE CODE ALONG WITH THE COMMENTS

CODE:

import java.util.Scanner; // Import the Scanner class

public class Main {

public static void main( String[] args ) {

// Create a Scanner object

Scanner myInput = new Scanner(System.in);

// Accept the user's input

System.out.println("Enter (3) digits: ");

int W = myInput.nextInt();

int X = myInput.nextInt();

int Y = myInput.nextInt();

int first = 0;

int second = 0;

int third = 0;
  
if(W>X && W>Y) //condition for w is greather.
{
if(X>Y)
{
first=Y;
second=X;
third=W;
}
else
{
first=X;
second=Y;
third=W;
}
}
else if(X>W && X>Y) //condition for x is greather.
{
if(W>Y)
{
first=Y;
second=W;
third=X;
}
else
{
first=W;
second=Y;
third=X;
}
  
}
else
{
if(W>X)
{
first=X;
second=W;
third=Y;
}
else
{
first=W;
second=X;
third=Y;
}
}

// Report the sorted numbers

System.out.println( "The sorted values are:" );

System.out.println( first + " " + second + " " + third);

}

}

OUTPUT:

SCREENSHOT OF THE CODE:


Related Solutions

------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input =...
------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int result = 0; System.out.print("Enter the first number: "); int x = input.nextInt(); System.out.print("Enter the second number: "); int y = input.nextInt(); System.out.println("operation type for + = 0"); System.out.println("operation type for - = 1"); System.out.println("operation type for * = 2"); System.out.print("Enter the operation type: "); int z = input.nextInt(); if(z==0){ result = x + y; System.out.println("The result is " + result); }else...
import chapter6.date.SimpleDate; import java.util.Scanner; public class SimpleDateTestDefault { public static void main(String[] args) { Scanner stdin...
import chapter6.date.SimpleDate; import java.util.Scanner; public class SimpleDateTestDefault { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); SimpleDate d1 = new SimpleDate(); SimpleDate d2 = new SimpleDate(stdin.nextInt(), stdin.nextInt(), stdin.nextInt()); System.out.println(d1); System.out.println(d2); System.out.println(d1.before(d2)); System.out.println(d2.before(d1)); } } Implement SimpleDate class in chapter6.date package with the following attributes: day, (int type,  private) month, (int type,  private) year (int type,  private) The class should have the following methods: a constructor with three parameters: year, month, and day a constructor with no parameters which initialize the...
Task 2/2: Java program Based upon the following code: public class Main {   public static void...
Task 2/2: Java program Based upon the following code: public class Main {   public static void main( String[] args ) {     String alphabet = "ABCDEFGHIJKLMNMLKJIHGFEDCBA";     for( <TODO> ; <TODO> ; <TODO> ) {       <TODO>;     } // Closing for loop   } // Closing main() } // Closing class main() Write an appropriate loop definition and in-loop behavior to determine if the alphabet string is a palindrome or not. A palindrome is defined as a string (or more generally, a token) which...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        } System.out.println(""+getAvg(new_stack));    }     public static int getAvg(Stack s) {        //TODO: Find the average of the elements in the...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) {...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) { Scanner input=new Scanner(System.in); int[] WordsCharsLetters = {0,1,2}; while(input.hasNext()) { String sentence=input.nextLine(); if(sentence!=null&&sentence.length()>0){ WordsCharsLetters[0] += calculateAndPrintChars(sentence)[0]; WordsCharsLetters[1] += calculateAndPrintChars(sentence)[1]; WordsCharsLetters[2] += calculateAndPrintChars(sentence)[2]; } else break; } input.close(); System.out.println("Words: " + WordsCharsLetters[0]); System.out.println("Characters: " + WordsCharsLetters[1]); System.out.println("Letters: " + WordsCharsLetters[2]); } static int[] calculateAndPrintChars(String sentence) { int[] WCL = new int[3]; String[] sentenceArray=sentence.split(" "); WCL[0] = sentenceArray.length; int letterCount=0; for(int i=0;i<sentence.length();i++) { if(Character.isLetter(sentence.charAt(i))) letterCount++; } WCL[1]...
Can you fix the errors in this code? import java.util.Scanner; public class Errors6 {    public...
Can you fix the errors in this code? import java.util.Scanner; public class Errors6 {    public static void main(String[] args) {        System.out.println("This program will ask the user for three sets of two numbers and will calculate the average of each set.");        Scanner input = new Scanner(System.in);        int n1, n2;        System.out.print("Please enter the first number: ");        n1 = input.nextInt();        System.out.print("Please enter the second number: ");        n2 =...
What is wrong with this code and how can it be fixed? import java.util.Scanner; public class...
What is wrong with this code and how can it be fixed? import java.util.Scanner; public class admissionRequirement { public static void main(String[] args) { // TODO Auto-generated method stub Scanner myObj = new Scanner(System.in); System.out.println("What is your name?"); String name = myObj.nextLine(); System.out.println("What is your Reading Score?"); int reading = myObj.nextInt(); System.out.println("What is your Math Score?"); int math = myObj.nextInt(); System.out.println("What is your Writing Score?"); int writing = myObj.nextInt(); System.out.println("What is your Class Standing?"); int standing = myObj.nextInt(); System.out.println("What is...
NEed UML diagram for this java code: import java.util.ArrayList; import java.util.Scanner; class ToDoList { private ArrayList<Task>...
NEed UML diagram for this java code: import java.util.ArrayList; import java.util.Scanner; class ToDoList { private ArrayList<Task> list;//make private array public ToDoList() { //this keyword refers to the current object in a method or constructor this.list = new ArrayList<>(); } public Task[] getSortedList() { Task[] sortedList = new Task[this.list.size()];//.size: gives he number of elements contained in the array //fills array with given values by using a for loop for (int i = 0; i < this.list.size(); i++) { sortedList[i] = this.list.get(i);...
import java.util.Scanner; public class Lab9Q3 { public static void main (String [] atgs) { double mass;...
import java.util.Scanner; public class Lab9Q3 { public static void main (String [] atgs) { double mass; double velocity; double totalkineticEnergy;    Scanner keyboard = new Scanner (System.in); System.out.println ("Please enter the objects mass in kilograms"); mass = keyboard.nextDouble();    System.out.println ("Please enter the objects velocity in meters per second: "); velocity = keyboard.nextDouble();    double actualTotal = kineticEnergy (mass, velocity); System.out.println ("Objects Kinetic Energy: " + actualTotal); } public static double kineticEnergy (double mass, double velocity) { double totalkineticEnergy =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT