Sei sulla pagina 1di 58

Chapter 2 Data Type,

Variable, & Operators


BIE1213 | Java Programming I
Prepared by Vinod Sarna

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Contents
1.
2.
3.
4.
5.
6.

Lecture 1 Review
Modifying The First Java Program
Primitive Data Types
Logical Operators and Assignments
Casting
Sample Codes

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Revision : Hello World


// HelloWorld.java
import java.lang.*; //the API or library to be imported
public class HelloWorld { // the class/file name
public static void main(String[] args){
System.out.println(World\n\n\t2013);
}
}

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Program Processing
Compilation
javac HelloWorld.java

Execution
java HelloWorld

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

public static void main(String args[])


public: The keyword public is an access specifier that
declares the main method as unprotected.
static: It says this method belongs to the entire class and
NOT a part of any objects of class. The main must always
be declared static since the interpreter users this before
any objects are created.
void: The type modifier that states that main does not
return any value.

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Primitive Data Types


Name

Range

Storage Size

byte

27 (-128) to 271 (127)

8-bit signed

short

215 (-32768) to 2151 (32767)

16-bit signed

int

231 (-2147483648) to 2311 (2147483647) 32-bit signed

long

263 to 2631
(i.e., -9223372036854775808
to 9223372036854775807)

64-bit signed

float

Negative range:
-3.4028235E+38 to -1.4E-45
Positive range:
1.4E-45 to 3.4028235E+38

32-bit IEEE 754

double

Negative range:
-1.7976931348623157E+308 to
-4.9E-324
Positive range:
4.9E-324 to 1.7976931348623157E+308

64-bit IEEE 754

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Identifiers
An identifier is a sequence of characters that consist
of letters, digits, underscores (_), and dollar signs
($).
An identifier must start with a letter, an underscore
(_), or a dollar sign ($).
It cannot start with a digit.
Should not have space in between.
An identifier cannot be a reserved word.
An identifier cannot be true, false, or null.
An identifier can be of any length.
ALL RIGHTS RESERVED
No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Identifiers
VALID identifiers
a1
num_1
$num
INVALID identifiers
1a
num 1
public
ALL RIGHTS RESERVED
No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Numeric Operators
Name

Meaning

Example

Result

Addition

34 + 1

35

Subtraction

34.0 0.1

33.9

Multiplication

300 * 30

9000

Division

1.0 / 2.0

0.5

Remainder

20 % 3

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Variables

Local Variables are declared within the block of code

Variable has a type preceding the name

Initial value is set by initialization expressions.

type variableName =
e.g.

10

int

initialValue;

x = 1;

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Declaring Variables
int x;

// Declare x to be an
// integer variable;

double radius; // Declare radius to


// be a double variable;
char m;

11

// Declare m to be a
// character variable;

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Declaring and Initializing in One Step


int x = 1;

//Assign 1 to x;

double d = 1.4;

//Assign 1.4 to d;

char a = A;

//Assign A to a;

float radius = 1.0f;//Assign 1.0 to radius;


String s2=12345;

12

//Assign 12345 to s2

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Constants
Constants are similar to variables except that they
hold a fixed value. They are also called READ only
variables.
final datatype CONSTANTNAME = VALUE;

Constants are
word final.

declared

with

the

reserved

final int MAX_LENGTH = 420;


final double PI = 3.1428;

By convention upper case letters are used for


defining constants.
13

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Declaring Constants Code Example


public class CircleArea {
public static void main(String[] args) {
final double PI = 3.1428;
double radius = 5.5;
double area;
area = PI * radius * radius;
System.out.println("Area= "+area);
}
}

14

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Expressions and Operators


Expressions are segments of code that perform
computations and return values.
They are
combinations of literals, variables, constants, and
operators.
Eg. = a - b or = 3 * 5 is an expression
An operator performs
constants, and literals.
Eg. + , - , *, % , /

an

action

on

variables,

Associated with an operator are one or two operands


that receive the action of the operator.
Eg. = (3 + 5) * 2
(3+5) first operand and 2 will be
the second operand

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Arithmetic Expression

y = mx + c ;
is translated to

y = m * x + c ;
16

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Arithmetic Expression

x = a(y * m);
3

is translated to

17

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Arithmetic Expression

x = a(y * m);
3

is translated to

x = a*(y*y*y * m);
18

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Arithmetic Expression

a = y(ax3 + c);
is translated to
Answer:

19

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Arithmetic Expression
3 4x 10(y 5)(a b c)
4 9 x

9(
)
5
x
x
y

is translated to
Answer:

20

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

How to Evaluate an Expression


Rules for order in which parts of mathematical
expression are evaluated first:

1.( ) - parentheses
2.*, / , %
3.+ , If the expressions occurs at the same time solve it
from left to right!
21

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

How to Evaluate an Expression


3 + 4 * 4 + 5 * (4 + 3) - 1
3 + 4 * 4 + 5 * 7 1
3 + 16 + 5 * 7 1
3 + 16 + 35 1
19 + 35 1
54 - 1
22

53

(1) inside parentheses first


(2) multiplication

(3) multiplication

(4) addition

(5) addition
(6) subtraction

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

How to Evaluate an Expression


Exercise 1
Find the value for each expression below:
a)= 56 + 23 10
b)= 20 + 6 * 3 5
c)= 40 (5 + 6 * 3) + 2
d)= 5 % 3 + 6 / 2 * (4 + 3)
23

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Shortcut Assignment Operators

24

Operator

Example

Equivalent

+=

i += 8

i = i + 8

-=

f -= 8.0

f = f - 8.0

*=

i *= 8

i = i * 8

/=

i /= 8

i = i / 8

%=

i %= 8

i = i % 8

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Shortcut Assignment Operators


Exercise 2
Find the value for each expression below:
Given y = 8;
a)y+ = 6;

e) y/ = 5 + 2 * 3;

b)y* = 2;

f) y*= 2*y;

c)y- = 12 y;
d)y% = 16 / y ;
25

g) y+ = 20/5 + 8 * 2;
h) y% = 80 / y - 1;

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Increment and Decrement Operators


There are two types of operators, one is increment and
another one is decrement operators.
Increment operator:

Postfix i++ is equal to i = i + 1 ;


Prefix ++i is equal to i = i + 1 ;
Decrement operator:

Postfix i-- is equal to i = i 1 ;


Prefix --i is equal to i = i 1 ;
26

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Increment and Decrement Operators

int i = 10;
int newNum = 10 * i++;

int i = 10;
int newNum = 10 * (++i);

27

Same effect as

Same effect as

int newNum = 10 * i;
i = i + 1;

i = i + 1;
int newNum = 10 * i;

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Increment and Decrement Operators


Exercise 3
What will be the output for the codes below:
class PrePostDemo {
public static void main(String[] args){
int i = 3;
System.out.println(i);
System.out.println(++i);
System.out.println(i++);
System.out.println(i);
}
}
28

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Increment and Decrement Operators


Exercise 4
What will be the output for the codes below:
class PrePostDemo {
public static void main(String[] args){
int i = 8;
System.out.println(--i);
System.out.println(i);
System.out.println(i--);
System.out.println(i);
}
}
29

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Increment and Decrement Operators


Exercise 5
What will be the output for the codes below:
class PrePostDemo {
public static void main(String[] args){
int i = 10;
System.out.println(++i);
System.out.println(i++);
System.out.print(i--);
System.out.println(i++);
}
}
30

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Increment and Decrement Operators


Exercise 6
What will be the output for the codes below:
class PrePostDemo {
public static void main(String[] args){
int i = 5;
System.out.print(--i + );
System.out.print(i-- + );
i = 10;
System.out.print(++i + );
System.out.println(i);
}
}31
ALL RIGHTS RESERVED
No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Increment and Decrement Operators


Exercise 7
What will be the output for the codes below:
class PrePostDemo {
public static void main(String[] args){
int i = 25;
System.out.println(i++);
System.out.println(i--);
i + = 5;
System.out.println(i*2);
System.out.println(++i);
}
}32
ALL RIGHTS RESERVED
No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

How to Evaluate an Expression


Exercise 8
Given x = 2 , y = 8;
a)a = 6 + x++ ;
b)m = 52 ( 2 * ++y) ;
c)y% = 16 / y++ ;
d)x- = 12 + y++ / --x ;
e)x+= y++ + x++ * ++x - ++y ;
33

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Casting

Forcing the conversion


Most general form of conversion.
Specified by a data type in parentheses, (type)
operand.
Its not automatic. Programmer must specify.

Example:
float money = 8.75f;
int dollars;
dollars = (int) money;

Casts money (float) to dollars (int).


dollars is 8 , money truncated (decimal portion
dropped)
ALL RIGHTS RESERVED
No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Type Casting

range increases
byte, short, int, long, float, double

Implicit casting
int e = 3;
double d = (double)e;
means d = 3.0 (type widening)

Explicit casting
float
int i
means
int j
means

x
=
i
=
j

= 3.0, y = 3.9;
(int)x;
= 3 (type narrowing)
(int)y;
= 3 (fraction part is truncated)

What is wrong? int x = 5 / 2.0;

35

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Casting Continued
Casting char variables
char y = 'A';
int c;
c = (int) y;
char y is cast as an int c is 65
int x = 97;
char m = (char) x;
int x is cast as char m = a
ALL RIGHTS RESERVED
No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Mixed Data/Variable Types


a) Widening conversions.
Safest, does not lose information.
Data conversion involves smaller to larger
storage.
From
To
byte

short, int, long, float, double

short

int, long, float, double

char

int, long, float, double

int

long, float, double

long*
float

float, double
double

* Some precision (significant digits) could be


lost!

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Mixed Data/Variable Types


b) Narrowing conversions.
Likely to lose information. Avoid!
Data conversion involves larger to smaller
storage.
From

To

byte

char

short

byte, char

char

byte, short

int

byte, short, char

long
float
double

byte, short, char, int


byte, short, char, int, long
byte, short, char, int, long, float

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

END OF PART 1 CHAP 2 2

39

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

More Java: Classes and static methods


// to compute square root of number
import java.lang.Math;
public class SquareRoot{
public static void main(String[] args){
double x = 4.0;
double y;
y = Math.sqrt(x);
System.out.println("y= "+y);
}
}

40

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

More Java: Classes and static methods


xy

// to compute x raised to the yth power


import java.lang.Math;
public class Power{
public static void main(String[] args){
double x = 5.0;
double y = 2.0;
double z = Math.pow(x,y);
System.out.println("z= "+z);
}
}

41

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

More Java: Classes and static methods


// to find the max value
import java.lang.Math;
public class BigValue{
public static void main(String[] args){
double x = 5.0;
double y = 2.0;
double z = Math.max(x,y);
System.out.println(The max value = "+z);
}
}

42

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Input
There are two ways of obtaining input from the user.
1. Using the Scanner class (console input)

2. Using JOptionPane input dialogs

43

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Scanner Class (Console Input)


//the API to import
import java.util.Scanner;
//the lines to declare an object using Scanner class
Scanner input = new Scanner(System.in);
int d = input.nextInt();

44

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Scanner Class (Console Input)


Types of methods in Scanner class:
1.nextInt();
2.nextDouble();
3.nextFloat();
4.nextLong();
5.nextShort();
6.nextByte();
7.nextBoolean();
45

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Sample Code : Using Scanner


import java.util.Scanner;
public class Calc {
public static void main(String[] args){
System.out.print("Enter an int value: ");
Scanner input = new Scanner(System.in);
int d = input.nextInt();
System.out.print("Enter an int value: ");
int e = input.nextInt();
int sum=d+e;
System.out.println("The total is = " +sum);
}
46
}
ALL RIGHTS RESERVED
No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

More Java: Classes and static methods


Exercise 1
a)Find the maximum value between 2 double input values from
the user.
b)Find the minimum value between 2 double input values from
the user.
c)Request the user to input 2 double values and compute x
raised to the yth power (xy)
47

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Getting Input from Input Dialog Boxes


String input = JOptionPane.showInputDialog("Enter an input");

48

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Getting Input from Input Dialog Boxes


String x = JOptionPane.showInputDialog(null,
Enter a year, Example 2.2 Input (int),
JOptionPane.QUESTION_MESSAGE);

49

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

The JOptionPanes Method

TypesofMessages:
a)ERROR_MESSAGEfordisplayinganerrormessage
b)INFORMATION_MESSAGEfordisplayingan
informationalmessage
c)QUESTION_MESSAGEfordisplayingaquery
message
d)WARNING_MESSAGEfordisplayingawarning
message
e)PLAIN_MESSAGEfordisplayinganyothertypeof
message
JAVA
BPROG120

50

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Converting Strings to Integers


The input returned from the input dialog box is a
string. If we enter a numeric value such as 123, it
returns 123. To obtain the input as a number, we
have to convert a string into a number.
To convert a string into an int value:
String y = JOptionPane.showInputDialog("Enter an input");
int x = Integer.parseInt(y);

51

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Converting Strings to Doubles


To convert a string into a double value, we can use
the static parseDouble method in the Double class
as follows:
To convert a string into a double value:
String y = JOptionPane.showInputDialog("Enter an input");
double x = Double.parseDouble(y);

52

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Sample Code : Using JOptionPane


import javax.swing.*;
public class Calc2{
public static void main(String[] args){
String a1= JOptionPane.showInputDialog(null,"Enter an
integer","Input",JOptionPane.QUESTION_MESSAGE);
int n1 = Integer.parseInt(a1);
String a2 = JOptionPane.showInputDialog(null,"Enter
an integer","Input",JOptionPane.QUESTION_MESSAGE);
int n2 = Integer.parseInt(a2);
int sum=n1+n2;
JOptionPane.showMessageDialog(null,"The total sum of
"+n1+" and "+n2+" = "
+sum,"Output",JOptionPane.INFORMATION_MESSAGE);
}
53
ALL RIGHTS RESERVED
No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide
}

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Problem 1 : Converting Temperatures


Write a program that converts a Fahrenheit
degree to Celsius using the formula. Use
console input :

54

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Problem 2 : Find the volume of a cuboid


Write a program that takes three double values
using JOptionPane input dialogs and find the
volume of a cuboid:

55

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Problem 3 : Find the area of a cuboid


Write a program that takes three double values
using JOptionPane input dialogs and find the
Total Surface Area (TSA) of a cuboid:

56

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

Problem 4 : Find the area of a circle


Write a program that takes a double value for
radius using JOptionPane input dialogs and
find the area of a circle:

57

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

FACULTY OF INFORMATION & COMMUNICATION TECHNOLOGY

BIE1213 | JAVA PROGRAMMING I

END OF CHAPTER 2

58

ALL RIGHTS RESERVED


No part of this document may be reproduced without written approval from Limkokwing University of Creative Technology Worldwide

Potrebbero piacerti anche