Question

In: Computer Science

USING JAVA (netbeans) In your main, ask the user for an int. Do this by first...

USING JAVA (netbeans)

In your main, ask the user for an int. Do this by first printing a request for the int, then creating an int x, and using the Scanner to read an int from the user and put it in x, like this:

int x = scan.nextInt();

☑ Next read in two doubles d1 and d2 from the user. This will look a lot like what you did for the int, but the scanner reads doubles using

scan.nextDouble();

☑ Next read in a string s from the user. Scanner reads strings using

scan.next();

Part B

☑ if x is below 12, double it, otherwise halve it; either way print the result.

☑ if s is "Hello" print "Hi there!" otherwise print "No hello? RUDE!"

☑ if d1 and d2 are the same, print "same" otherwise print whichever of d1 or d2 is higher, with the word "MAX:" in front of it

Solutions

Expert Solution

CODE -

package sample;
import java.util.Scanner;
public class Sample {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter an integer: "); // Prompting user to enter an integer
int x = scan.nextInt(); // Reading an integer from user and storing it in x
System.out.print("Enter a double: "); // Prompting user to enter a double
double d1 = scan.nextDouble(); // Reading a double from user and storing it in d1
System.out.print("Enter another double: "); // Prompting user to enter another double
double d2 = scan.nextDouble(); // Reading an double from user and storing it in d2
System.out.print("Enter a string: "); // Prompting user to enter a string
String s = scan.next(); // Reading a string from user and storing it in s
if(x<12)
x = x*2; // Multiplying x by 2 if x is below 12
else
x = x/2; // Dividing x by 2 otherwise
System.out.printf("x = %d\n", x); // Printing the result
if(s.equals("Hello"))
System.out.println("Hi there!"); // Printing "Hi there!" if user entered "Hello"
else
System.out.println("No hello? RUDE!"); // Printing another message otherwise
if(d1 == d2)
System.out.println("same"); // Printing "same" if d1 = d2
// Printing max of the two values otherwise
else if(d1 > d2)
System.out.printf("MAX: %.2f\n", d1);
else
System.out.printf("MAX: %.2f\n", d2);
}
}

SCREENSHOT -

If you have any doubt regarding the solution, then do comment.

Do upvote.


Related Solutions

.......Subject Java..... main() main() will ask the user for input and then call functions to do...
.......Subject Java..... main() main() will ask the user for input and then call functions to do calculations. The calculations will be returned to main() where they will be printed out. First function Create a function named computeBill that receives on parameter. It receives the price of an item. It will then add 8.25% sales tax to this and return the total due back to main(). Second function Create another function named computeBill that receives 2 parameters. It will receive the...
in Java using netbeans create a project and in it a class with a main. We...
in Java using netbeans create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste import java.util.Scanner; Part A ☑ In your main method, paste this code. Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?");...
in Java using netbeans create a project and in it a class with a main. We...
in Java using netbeans create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste import java.util.Scanner; Part A ☑ In your main method, paste this code. Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?");...
Java Codes: 1. Create a class named Proficiency1Practice In the main method, first ask the user...
Java Codes: 1. Create a class named Proficiency1Practice In the main method, first ask the user to enter a short sentence which ends in a period. Use Java String class methods to determine the following about the sentence and display in the console: • How many total characters are in the sentence? • What is the first word of the sentence? Assume the words are separated by a space. • How many characters are in the first word of the...
Write a Java program to do the following: Ask the user to enter 10 first names...
Write a Java program to do the following: Ask the user to enter 10 first names (one word - no hyphen or apostrophe) using the keyboard. 1) Display the list of names one per line on the Console. 2) Display the names again, after eliminating duplicates using a HashSet (Your code MUST use HashSet).
Create a Netbeans project called LineNumbers The program should do the following: –Ask the user for...
Create a Netbeans project called LineNumbers The program should do the following: –Ask the user for how many lines of text they wish to enter –Declare and initialize an array of Strings to hold the user’s input –Use a while loop to prompt for and read the Strings (lines of text) from the user at the command line. Typically, this would be a 'for' loop since we know the number of times to execute the loop based upon the number...
using C language Create a bitwise calculator that ask user for input two input, first int,...
using C language Create a bitwise calculator that ask user for input two input, first int, bitwise operation, second int (i.e 5 & 9) only those bitwise operation are allowed: & ~ ^ | << >>. If user uses wrong operators stop program and ask again. Convert the first int and second int into 8 BITS binary (00000000) and use bitwise operator given by user to either AND, OR, XOR, etc (ie 1001 & 1111)
in netbeans using Java Create a project and a class with a main method, TestCollectors. ☑...
in netbeans using Java Create a project and a class with a main method, TestCollectors. ☑ Add new class, Collector, which has an int instance variable collected, to keep track of how many of something they collected, another available, for how many of that thing exist, and a boolean completist, which is true if we want to collect every item available, or false if we don't care about having the complete set. ☑ Add a method addToCollection. In this method,...
IN JAVA PLEASE ASAP !!! I just need the main and mergesort function Ask the user...
IN JAVA PLEASE ASAP !!! I just need the main and mergesort function Ask the user for the number of elements, not to exceed arraySize = 20 (put appropriate input validation) Ask the user for the type of data they will enter - EnglishGrade or MathGrade objects. Use your EnglishGrade and MathGrade classes Based on the input, create an appropriate array for the data to be entered. Write a helper function called recursionMergeSort such that: It is a standalone function...
Write a Java program that will first ask the user how many grades they want to...
Write a Java program that will first ask the user how many grades they want to enter. Then use a do…while loop to populate an array of that size with grades entered by the user. Then sort the array. In a for loop read through that array, display the grades and total the grades. After the loop, calculate the average of those grades and display that average. Specifications Prompt the user for the number of grades they would like to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT