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

Lecture6-2024

This document covers fundamental data types in Java, focusing on Java functions, the Java Math Library, and reading user input from the keyboard. It outlines learning outcomes, provides examples of Java functions, and explains how to use the Scanner class for input. Additionally, it includes exercises for practical application of the concepts discussed.

Uploaded by

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

Lecture6-2024

This document covers fundamental data types in Java, focusing on Java functions, the Java Math Library, and reading user input from the keyboard. It outlines learning outcomes, provides examples of Java functions, and explains how to use the Scanner class for input. Additionally, it includes exercises for practical application of the concepts discussed.

Uploaded by

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

COMPUTER

SCIENCE

CSI141
PROGRAMMING
h t t p s : / / h o r s t m a n n . c o m / b j l o / i n d e x . ht m l PRINCIPLES
Programming Principles T ALLMAN NKGAU

L ECTURE 6 – F UNDAMENTAL
D ATA TYPES
‣ Java Functions
‣ The Java Math Library
‣ Reading numbers from the
keyboard

https://horstmann.com/bjlo/index.html
L EARNING OUTCOMES

At the end of the lecture, you should be able

to:

‣ Identify formal parameters, name, and return type of

a Java function

‣ Use the Java Math class functions in expressions

‣ Create a Scanner object to read from the keyboard

integer and double values

‣ Convert Math expressions to equivalent Java


h t t p : / / ho r st m a n n . c o m / b j l o / i n d e x . ht m l

expressions/statements and vice-versa

‣ Write a Java program that reads user input (numbers)

and performs computations


‣ Java Functions
‣ The Java Math Library
‣ Reading numbers from the
keyboard

h t t p s : / / h o r s t m a n n . c o m / b j l o / i n d e x . ht m l
Java Function

❑ A code block with a name


▪Create a function first
▪Call/Invoke the method to execute its code block
❑ Why?
✓Promotes modular programming/development
✓Makes it easier to share and reuse code Input type
✓Makes it easier to maintain code
Type of value returned Function name

public static boolean isPrime( int n) {


}

Required to create a Java function

5
Java Function

public static double average( int x, int y ) {


int sum = x + y;
double average = sum / 2.0;
return average;
}

Formal parameters
Type of value returned
Function name
❑ x and y are used to hold values passed to
the function. Two integers must be
passed/given to the function average .
int a = 55;
double avg = average(45, a);
❑ The function average returns a value, a
double value (a real number).
6
‣ Functions
‣ The Java Math Library
‣ Reading numbers from the
keyboard

h t t p : / / ho r st m a n n . c o m / b j l o 1/ i n de x . h t m l
Java Library – Java Math Library

❑ A Java class

With static methods – functions
➢ Tested, efficient, and made available to be used by
others

Name of function
Formal parameters Purpose of function
A double value
is returned

https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Math.html

The Java Math library declares many


other functions.
8
Java Math Library
❑ Provides useful functions (methods)

Page 9
Powers and Roots
In Java, there are no symbols for powers and
roots
– Becomes:

b * Math.pow(1 + r / 100, n)

Analyzing the expression:

By convention, ALL Java classes must


start with an uppercase letter.
This applies to ALL the classes we write
in this course!
Arithmetic Expressions

Page 11
POP QUIZ
Assume x = 17, y = 15 and that x, y, z are int
variables, while w and t are double variables.

1. Evaluate/execute the following:


a) x + y/4
b) (x + y)/4
c) z = x % 3 + 4
d) w = 17/3 + 6.5
e) t = x/4.0 + 15 % 4 − 3.5
2. Convert the following to Java expressions:
a) sin2 3𝑥 − cot(2𝑥)

b) (−𝑏 + 𝑏 2 − 4𝑎𝑐)/2𝑎

c) tan(5𝑥)

Page 12
Example – with Math functions

❑ Write a Java program to compute the volume of a cylinder with radius


2.5cm and height of 10.0cm

public class CylinderVolume {


public static void main(String[] args) {
double radius = 2.5;
double height = 10.0;
double volume = Math.PI * Math.pow(radius, 2) * height;
System.out.printf(“Volume: %.2f\n“, volume);

}
}

𝝅 𝑉 = 𝜋r ℎ 2
‣ Functions
‣ The Java Math Library
‣ Reading numbers from the
keyboard

h t t p s : / / h o r s t m a n n . c o m / b j l o / i n d e x . ht m l
Reading input
This is a three-step process in Java

1. Import the Scanner class from its ‘package’java.util


import java.util.Scanner;

2. Setup an object of the Scanner class


Scanner in = new Scanner(System.in);

3. Use methods of the new Scanner object to get input


int bottles = in.nextInt(); // reading an integer
double price = in.nextDouble(); // reading a real number

15
Other reading methods

❑ nextInt() – to read and return an int value

❑ nextByte() – to read and return byte value

❑ nextFloat() – to read and return a float


value
❑ nextShort() – to read and return a short
value
❑ nextLong() – to read and return a long value

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Scanner.html

16
Exercise

Write a Java program to compute the volume of


a cylindrical object (𝑉 = 𝜋 × 𝑟 2 × ℎ). Your
program must read the radius and height of the
cylinder from the keyboard.

17
Solution – analysis and design

❑ Recall: input – process – output


❑ What is our input? radius and height

❑ What is the data type of each input?


radius is double
height is double

❑ What processing must we do?


compute volume = 𝜋 × 𝑟𝑎𝑑𝑖𝑢𝑠 2 × ℎ𝑒𝑖𝑔ℎ𝑡

❑ What output is required?


The volume computed 18
Solution – the program
// Class header comment goes here
import java.util.Scanner; // import the Scanner class

public class VolumeOfCylinder {


public static void main(String[] args) {
// declare variables to hold data
double radius;
double height;
double volume;

// create a Scanner object to read from the keyboard


Scanner in = new Scanner(System.in);

19
Solution – the program
// prompt for radius and read it
System.out.printf(“Enter radius: “); // prompt
radius = in.nextDouble(); // read a double

// prompt for height and read it


System.out.printf(“Enter height: “); // prompt
height = in.nextDouble(); // read a double

// processing
volume = Math.PI * radius * radius * height;

// output
System.out.printf(“Volume = %.2f\n”, volume);
}
}

20
Summary

•Use compound operators including pre and post


decrement operators
•Use the Scanner class to read numbers from the
console/keyboard

Page 21
Exercises

1. E2.3 – E2.8
2. P2.3
3. P2.7

Page 22

You might also like