Lesson 4: Worksheet Two: Introduction to Java (Input/Output)

แชร์
ฝัง
  • เผยแพร่เมื่อ 29 ก.ย. 2024
  • Learn about input/output operations in Java, covering console and dialog box interactions.
    Question #1 (Using Console)
    Write a program that reads the marks of three students from the console, then calculates and displays their average using the console too. Hint: use the following piece of code: Scanner s = new Scanner (System.in); int h=s.nextInt(); Do not forget to import java.util.Scanner;
    Question #2 (Reading characters): Write a program that reads any character using the console and then prints the corresponding Ascii code. For example, if the user enters 'a', the output will be 97. Rewrite the program using dialog boxes.
    Question #3 (Using Dialog Boxes) Write a program that reads a Fahrenheit degree in double from an input dialog box, then converts it to Celsius and displays the result in a message dialog box. Hint: celsius = (5/9) * (fahrenheit - 32), use the following piece of code: String s = JOptionPane.showInputDialog(null,Enter the Fahrenheit degree"); double fahrenheit =Double.parseDouble(s); do not forget to import javax.swing.JOptionPane ; rewrite the program using console.
    Question #4 (Math Class): Write a program that reads the radius of a circle using dialog boxes, then calculates and displays its area. Hint : cir _area =PI*radius^2, use Math.PI constant and Math.pow(double,double) method. Rewrite the program using console.

ความคิดเห็น •