Premium

School

Sheridan College
Department

Computer Engineering Technology

Course Code

PROG 10082

Professor

Dancy

Java Lecture 4

In the expressions examples slide, there's 3 variables.

mathematical methods functions are used to tell the computer how to calculate things,

such as Math.pow(x,y);

Math is a class, and the method is coming from the class, so in thix example Pow would

be the power class

-math.sqrt would give us the second root of a number, as in squared

-math.pow(8, 1/3)--> make sure to write the 1/3 as 1.0/3

Larger math expressions

double result=Math.sqrt((102.0+79.0-576.7)/3927.0);

System.out.println("%.2d, result);

Note that if you calculate the value of this expression, you get a negative value. There is

no way to find the second root of a negative value. We are finding the sqrt of a negative

number and java cant read it.

type casting changes the value of a variable to another, and it is not recommended unless

mandatory

-implicit typecasting ex. 3, you cant change two variables at the same time-see slide

saying Example

Class Example: Find the average of two integers

age 1+ age 2/2 is the mathematical version

int age1=3;

int age 2=5;

*when you are dealing with division the result should be a floating point. because when

you have different numbers you need the right answer mathematically.

double avg;

avg=(age1+age2)/2;

two ways to change that are to change the 2 to 2.0 or to change the age1 and age2 to

doubles. At least one of the inputs must be a double so that the output can be a double

public class ProgramV

{ // The mathematical constant Pi // Note: 'static' is needed here to use it from main

method

private static double pi;

public static void main(String[] args)

{ // This writes a message to the screen

Over 90% improved by at least one letter grade.

OneClass has been such a huge help in my studies at UofT especially since I am a transfer student. OneClass is the study buddy I never had before and definitely gives me the extra push to get from a B to an A!

Leah â€” University of Toronto

Balancing social life With academics can be difficult, that is why I'm so glad that OneClass is out there where I can find the top notes for all of my classes. Now I can be the all-star student I want to be.

Saarim â€” University of Michigan

As a college student living on a college budget, I love how easy it is to earn gift cards just by submitting my notes.

Jenna â€” University of Wisconsin

OneClass has allowed me to catch up with my most difficult course! #lifesaver

Anne â€” University of California

Join OneClass

Access over 10 million pages of study

documents for 1.3 million courses.

Sign up

Join to view

OR

By registering, I agree to the
Terms
and
Privacy Policies

Already have an account?
Log in

Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.