0% found this document useful (0 votes)
24 views

20 3 14159 "The Area For The Circle of Radius " " Is "

The document contains 9 code examples demonstrating the use of various Java classes and methods for tasks such as calculating areas and converting between units, reading input from the user, working with dates and times, and formatting output. The examples cover basic concepts like variables, conditionals, loops, methods, and classes.

Uploaded by

Blue37
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

20 3 14159 "The Area For The Circle of Radius " " Is "

The document contains 9 code examples demonstrating the use of various Java classes and methods for tasks such as calculating areas and converting between units, reading input from the user, working with dates and times, and formatting output. The examples cover basic concepts like variables, conditionals, loops, methods, and classes.

Uploaded by

Blue37
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

ONE: public class ComputeArea { public static void main(String[] args) { double radius; double area; radius = 20 area

= radius * radius * 3.14159; System.out.println("The area for the circle of radius " + radius + " is " + area); } TWO: public class DisplayTime { public static void main(String[] args) { int seconds = 500; int minutes = seconds / 60; int remainingSeconds = seconds % 60; System.out.println(seconds + " seconds is " + minutes + " minutes and " + remainingSeconds + " seconds"); } } THREE: public class FahrenheitToCelsius { public static void main(String[] args) { double fahrenheit = 100; double celsius = (5.0 / 9) * (fahrenheit - 32); System.out.println("Fahrenheit " + fahrenheit + " is " + celsius + " in Celsius"); } } FOUR: public class SalesTax { public static void main(String[] args) { double purchaseAmount = 197.55; double tax = purchaseAmount * 0.06; System.out.println((int)(tax * 100) / 100.0); } } FIVE: import javax.swing.JOptionPane; public class DisplayUnicode { public static void main(String[] args) { JOptionPane.showMessageDialog(null, "\u6B22\u8FCE \u03b1 \u03b2 \u03b3", "\u6B22\u8FCE Welcome", JOptionPane.INFORMATION_MESSAGE); } }

SIX: import java.util.Scanner; public class ComputeLoan { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter yearly interest rate, for example 8.25: "); double annualInterestRate = scanner.nextDouble(); double monthlyInterestRate = annualInterestRate / 1200; System.out.print( "Enter number of years as an integer, for example 5: "); int numberOfYears = scanner.nextInt(); System.out.print("Enter loan amount, for example 120000.95: "); double loanAmount = scanner.nextDouble(); double monthlyPayment = loanAmount * monthlyInterestRate / (1 - 1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12)); double totalPayment = monthlyPayment * numberOfYears * 12; monthlyPayment = (int)(monthlyPayment * 100) / 100.0; totalPayment = (int)(totalPayment * 100) / 100.0; System.out.println("The monthly payment is " + monthlyPayment); System.out.println("The total payment is " + totalPayment); } }

SEVEN: import java.util.Scanner; public class ComputeChange { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print( "Enter an amount in double, for example 11.56: "); double amount = scanner.nextDouble(); int remainingAmount = (int)(amount * 100); int numberOfOneDollars = remainingAmount / 100; remainingAmount = remainingAmount % 100; int numberOfQuarters = remainingAmount / 25; remainingAmount = remainingAmount % 25; int numberOfDimes = remainingAmount / 10; remainingAmount = remainingAmount % 10; int numberOfNickels = remainingAmount / 5; remainingAmount = remainingAmount % 5; int numberOfPennies = remainingAmount; String output = "Your amount " + amount + " consists of \n" + "\t" + numberOfOneDollars + " dollars\n" + "\t" + numberOfQuarters + " quarters\n" + "\t" + numberOfDimes + " dimes\n" + "\t" + numberOfNickels + " nickels\n" + "\t" + numberOfPennies + " pennies"; System.out.println(output); } }

EIGHT: public class ShowCurrentTime { public static void main(String[] args) { long totalMilliseconds = System.currentTimeMillis(); long totalSeconds = totalMilliseconds / 1000; int currentSecond = (int)(totalSeconds % 60); long totalMinutes = totalSeconds / 60; int currentMinute = (int)(totalMinutes % 60); long totalHours = totalMinutes / 60; int currentHour = (int)(totalHours % 24); System.out.println("Current time is " + currentHour + ":" + currentMinute + ":" + currentSecond + " GMT"); } } NINE: import java.util.Scanner; public class TestScanner { public static void main(String args[]) { Scanner scanner = new Scanner(System.in); System.out.print("Enter an integer: "); int intValue = scanner.nextInt(); System.out.println("You entered the integer " + intValue); System.out.print("Enter a double value: "); double doubleValue = scanner.nextDouble(); System.out.println("You entered the double value " + doubleValue); System.out.print("Enter a string without space: "); String string = scanner.next(); System.out.println("You entered the string " + string); } }

You might also like