















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
chapter 2 gf4rtgretg4geg5etryhntjhntggfeftrv
Typology: Exercises
1 / 55
This page cannot be seen from the preview
Don't miss anything!
















































١١
٢٢
Objectives ■ To write Java programs to perform simple computations (§2.2). ■ To obtain input from the console using the Scanner class (§2.3). ■ To use identifiers to name variables, constants, methods, and classes (§2.4). ■ To use variables to store data (§§2.5–2.6). ■ To program with assignment statements and assignment expressions (§2.6). ■ To use constants to store permanent data (§2.7). ■ To name classes, methods, variables, and constants by following their naming conventions(§2.8). ■ To explore Java numeric primitive data types: byte, short, int, long, float, and double (§2.9.1). ■ To perform operations using operators +, -, *, /, and % (§2.9.2). ■ To perform exponent operations using Math.pow(a, b) (§2.9.3). ■ To write integer literals, floating-point literals, and literals in scientific notation (§2.10). ■ To write and evaluate numeric expressions (§2.11). ■ To obtain the current system time using System.currentTimeMillis() (§2.12). ■ To use augmented assignment operators (§2.13). ■ To distinguish between postincrement and preincrement and between postdecrement andpredecrement (§2.14). ■ To cast the value of one type to another type (§2.15). ■ To describe the software development process and apply it to develop the loan paymentprogram (§2.16). ■ To represent characters using the char type (§2.17). ■ To represent a string using the String type (§2.18). ■ To obtain input using the JOptionPane input dialog boxes (§2.19).
٤٤
٥٥
٧٧
٨٨
Reading Input from the Console, CONT. importimport^ java.util.Scannerjava.util.Scanner
; // Scanner is in the; // Scanner is in the
java.utiljava.util^
packagepackage
public classpublic class
ComputeAreaComputeArea
{{
public static void public static void
main(Stringmain(String
[]^ args) {[]args) {
// Create a Scanner object// Create a Scanner object^ Scanner input = newScanner input = new
Scanner(System.inScanner(System.in
););
// Prompt the user to enter a radius// Prompt the user to enter a radius^ System.out.print("EnterSystem.out.print("Enter
a number for radius: ");a number for radius: ");
double radius =double radius =
input.nextDoubleinput.nextDouble
();();
// Compute area // Compute areadouble area = radius * double area = radius *
radius^ * 3.14159;radius* 3.14159; // Display results // Display resultsSystem.out.println("The^ System.out.println("The
area for the circle of radius " +area for the circle of radius " + ^ This program computes the area of the circle with console input^ radius + " is " + area);radius + " is " + area); }} } }
١٠١٠
١١١١
١٣١٣
:
١٤١٤
Named Constants AND NamingConventions^ final
double
PI
=^
3.14159;
final
double
PI
=^
3.14159;
final^ final
intint
SIZE
=^
3; SIZE
=^
3;
A named constant is an identifier that represents a permanent value. Here is the syntax for declaring a constant: final^ final
datatypedatatype
CONSTANTNAME = value;CONSTANTNAME = value; Here are some examples of
:
Naming conventions are a sticking with the Java namingconventions makes your programs easy to read and avoids errors.
١٦١٦
Numeric Operators Name^
Meaning
Example
Result
+^
Addition
34 + 1
35
-^
Subtraction
34.0 – 0.
*^
Multiplication
300 * 30
9000
/^
Division
1.0 / 2.
%^
Remainder
20 % 3
2
١٧١٧
Integer Division +, -, *, /, and %5 / 2 yields an integer 2.5.0 / 2 yields a double value 2.55 % 2 yields 1 (the remainder of the division)-7 % 3 yields -1, -12 % 4 yields 0, -26 % -8 yields -2,and 20 % -13 yields 7.
١٩١٩
java.util.Scanner; public class
DisplayTime { public static void
main(String[] args) { // Prompt the user for inputScanner input =
new^ Scanner(System.in); System.out.print("Enter an integer for seconds: "); int^ seconds = input.nextInt(); int^ minutes = seconds / 60; int^ remainingSeconds
= seconds % 60;
System.out.println(seconds + " seconds is " + minutes +" minutes and " + remainingSeconds + " seconds");} }
٢٠٢٠