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

Chapter3-Numerical Data and Expression (Part 2)

Here are the programs to solve the problems: 1. import java.util.Scanner; public class ProductOfTwoNumbers { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Input first number: "); int num1 = scan.nextInt(); System.out.print("Input second number: "); int num2 = scan.nextInt(); int product = num1 * num2; System.out.println("Product is: " + product); } } 2. import java.util.Scanner; public class SumOfDigits { public static void main(String[] args)

Uploaded by

Aziemah Haidah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Chapter3-Numerical Data and Expression (Part 2)

Here are the programs to solve the problems: 1. import java.util.Scanner; public class ProductOfTwoNumbers { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Input first number: "); int num1 = scan.nextInt(); System.out.print("Input second number: "); int num2 = scan.nextInt(); int product = num1 * num2; System.out.println("Product is: " + product); } } 2. import java.util.Scanner; public class SumOfDigits { public static void main(String[] args)

Uploaded by

Aziemah Haidah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Week 4

Numerical Data and


Expression (Part 2)

STIA1113 : Programming I
NUMERICAL COMPUTATION &
EXPRESSION (part 2)
• This topic focuses on:
– data conversions
– complex arithmetic computation
– accepting input from the user
Data Conversion
• Sometimes it is convenient to convert data
from one type to another
• For example, in a particular situation we
may want to treat an integer as a floating
point value
• These conversions do not change the type of
a variable or the value that's stored in it –
they only convert a value as part of a
computation
Data Conversion
• Widening conversions are safest because they tend to
go from a small data type to a larger one (such as a
short to an int)
• Narrowing conversions can lose information
because they tend to go from a large data type to a
smaller one (such as an int to a short)
• In Java, data conversions can occur in three ways:
– assignment conversion
– promotion
– casting
Data Conversion

Widening Conversions Narrowing Conversions


Assignment Conversion
• Assignment conversion occurs when a value of
one type is assigned to a variable of another
• Example:
int dollars = 20;
double money = dollars;
• Only widening conversions can happen via
assignment
• Note that the value or type of dollars did not
change
Promotion
• Promotion happens automatically when
operators in expressions convert their
operands
• Example:
int count = 12;
double sum =
490.27;
result = sum /
count;
• The value of count is converted to a
Casting
• Casting is the most powerful, and dangerous, technique for
conversion
• Both widening and narrowing conversions can be
accomplished by explicitly casting a value
• To cast, the type is put in parentheses in front of the value
being converted
int total = 50;
float result = (float) total / 6;
• Without the cast, the fractional part of the answer would be
lost
The Math class
• Math class: contains methods like sqrt and
pow
• To compute xn, you write Math.pow(x,
n)
• However, to compute x2 it is significantly
more efficient simply to compute x * x
• To take the square root of a number, use the
Math.sqrt; for example, Math.sqrt(x)
The Math class
• In Java,

can be represented as
(-b + Math.sqrt(b * b - 4 * a * c)) / (2 * a)
Mathematical Methods in Java
Math.sqrt(x) square root

Math.pow(x, y) power xy

Math.exp(x) ex

Math.log(x) natural log


Math.sin(x), Math.cos(x), sine, cosine, tangent (x in radian)
Math.tan(x)
Math.round(x) closest integer to x
Math.min(x, y), Math.max(x, y) minimum, maximum
Analyzing an Expression

Analyzing an Expression
Interactive Programs
• Programs generally need input on which to operate
• The Scanner class provides convenient methods
for reading input values of various types
• A Scanner object can be set up to read input from
various sources, including the user typing values on
the keyboard
• Keyboard input is represented by the System.in
object
Reading Input
• The following line creates a Scanner object that
reads from the keyboard:
Scanner scan = new Scanner
(System.in);
• The new operator creates the Scanner object
• Once created, the Scanner object can be used to
invoke various input methods, such as:
answer = scan.nextLine();
Reading Input
• The Scanner class is part of the java.util class
library, and must be imported into a program to be
used
• The nextLine method reads all of the input until the
end of the line is found
• See Echo.java
• The details of object creation and class libraries are
discussed further in Chapter 3
//********************************************************************
// Echo.java Author: Lewis/Loftus
//
// Demonstrates the use of the nextLine method of the Scanner class
// to read a string from the user.
//********************************************************************

import java.util.Scanner;

public class Echo


{
//-----------------------------------------------------------------
// Reads a character string from the user and prints it.
//-----------------------------------------------------------------
public static void main (String[] args)
{
String message;
Scanner scan = new Scanner (System.in);

System.out.println ("Enter a line of text:");

message = scan.nextLine();

System.out.println ("You entered: \"" + message + "\"");


}
}
//********************************************************************
// Echo.java Author: Lewis/Loftus
//
// Demonstrates the use of the nextLine method of the Scanner class
// to read a string from the user.
//********************************************************************

import java.util.Scanner;

public class Echo


{
//-----------------------------------------------------------------
// Reads a character string from the user and prints it.
//-----------------------------------------------------------------
public static void main (String[] args)
{
String message;
Scanner scan = new Scanner (System.in);

System.out.println ("Enter a line of text:");

message = scan.nextLine();

System.out.println ("You entered: \"" + message + "\"");


}
} Sample Run

Enter a line of text:


You want fries with that?
You entered: "You want fries with that?"
Input Tokens
• Unless specified otherwise, white space is used to
separate the elements (called tokens) of the input
• White space includes space characters, tabs, new
line characters
• The next method of the Scanner class reads the
next input token and returns it as a string
• Methods such as nextInt and nextDouble
read data of particular types
• See GasMileage.java
//********************************************************************
// GasMileage.java Author: Lewis/Loftus
//
// Demonstrates the use of the Scanner class to read numeric data.
//********************************************************************

import java.util.Scanner;

public class GasMileage


{
//-----------------------------------------------------------------
// Calculates fuel efficiency based on values entered by the
// user.
//-----------------------------------------------------------------
public static void main (String[] args) Sample Run
{
int miles; Enter the number of miles: 328
double gallons, mpg; Enter the gallons of fuel used: 11.2
Miles Per Gallon: 29.28571428571429
Scanner scan = new Scanner (System.in);

System.out.print ("Enter the number of miles: ");


miles = scan.nextInt();

System.out.print ("Enter the gallons of fuel used: ");


gallons = scan.nextDouble();

mpg = miles / gallons;

System.out.println ("Miles Per Gallon: " + mpg);


}
}
Self Test

1. Write a Java program that takes two numbers as input and display
the product of two numbers.
Test Data:
Input first number: 25
Input second number: 5

2. Write a Java program that reads an integer between 0 and 1000 and
adds all the digits in the integer.
Test Data
Input an integer between 0 and 1000: 565
Expected Output :
The sum of all digits in 565 is 16

You might also like