Unit 5: Discussion - Part 1
No unread replies.No replies.
Question
Read the lecture for Chapter 5 and then answer the following:.
Among other topics, this week's lecture covered Application
Programming Interface (API) and Wrapper classes. This discussion
thread will require material from both of them. Show
syntactically correct Java code that requests at least one number
from the user. The program will store all the numbers the user
provides into separate String variables, and it will use the
appropriate methods from the wrapper classes to convert these
Strings into numerical values. Afterwards, the
program should use these numerical values to
compute any mathematical formula that uses at
least one method from the Math class and display the outcome with
appropriate titles. Show also a screen capture with a sample
input/output session of your program.
Hint: Figure 5.3 from the textbook shows an
example that is similar to what is requested here. The only
difference is that, in the textbook, figure 5.3 reads the base of
the triangle using the nextDouble method. However, in this
discussion thread, you must use the nextLine method to read the
user input into a String and then use the wrapper class
parseDouble to convert the String into a double. Like this:
Scanner stdIn = new
Scanner(System.in);
String baseStr;
double base;
System.out.print("Enter the Width of the
Square: ");
baseStr = stdIn.nextLine();
base = Double.parseDouble(baseStr);
Come up with a similar example of your own.
SOURCE CODE:
*Please follow the comments to better understand the code.
**Please look at the Screenshot below and use this code to copy-paste.
***The code in the below screenshot is neatly indented for better understanding.
import java.util.Scanner;
class Main
{
public static void main (String[] args)
{
//take the scanner
Scanner sc=new Scanner(System.in);
// we will calculate the circumference and area of the
circle
System.out.print("Enter Radius of
the Circle: ");
String radiusStr = sc.nextLine();
// read as string
//parse it here with a
(WRAPPER)
Integer radius =
Integer.parseInt(radiusStr);
// Calculate the circumference and
area
// area = PI * r^2
double area = Math.PI *
Math.pow(radius,2);
double circumference = 2 * Math.PI
* radius;
System.out.println("Area:
"+area);
System.out.println("circumference:
"+circumference);
}
}
====================
SCREENSHOT:
Get Answers For Free
Most questions answered within 1 hours.