Download presentation
Presentation is loading. Please wait.
Published byMillicent Dean Modified over 9 years ago
1
Chapter 4 10/26 & 10/27
2
More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file
3
Chapter Topics Constants Standard Mathematical Methods Casting
4
4.1 Named Constants
5
Constants Unnamed constants Literal values – integers, floating-point, boolean E format may be used for doubles 1.5e2 --> 150 8e4 - decimal maybe left off, still a double 5.1e-4 = ?
6
Named Constants Similar to declaring variables, but values can't be changed. Use reserved word final Gives descriptive name, avoids typing mistakes All caps by convention final double TAX =.06; final int DOZEN = 12; final boolean NOT_DONE = false; Example in Einstein class.
7
Another Example Write a program to input the cost of a car in dollars. Output the sales tax on the car. Tax rate is 6% Use a constant. 1.Set taxRate 2.Input cost – use a Scanner 2. Find tax 3. Output tax.
8
4.3 Using Math Methods
9
Standard Mathematical Methods Java provides a collection of methods to do mathematical tasks Math is the name of the class Call to methods is Math.method_name( … ) Examples Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010
10
Standard Mathematical Methods Arguments and return type are double: Math.sqrt(x) -- square root of x Math.pow(x,y) – x to the y power Math.log(x) – ln x Math.log10(x) – log 10 x Math.exp(x) – e x Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010
11
Standard Mathematical Methods These return same type as argument: Math.abs(x) -- |x| Math.max(x,y) – maximum of x & y Math.min(x,y) – minimum of x & y Math.round(x) – Returns the nearest whole number to x. If x is double it returns a long. If x is float it returns an int Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010
12
Standard Mathematical Methods There more methods, but these are the only ones that will be required for this course. Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010
13
Standard Mathematical Methods Call to method Math.sqrt Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010
14
Example-Quarterback Ages A QuarterBack class represents a quarterback on a team. Write a method to output the difference of this Quarterback's and another one that is passed. Output the difference ages as a positive number. 1.diff = | this age – other age| 2.Output diff
15
Participation What is wrong with the following? a.) y = (1/2) * Math.sqrt(x); b.) y = sqrt(38.0); c.) y = Math.exp(2,3); d.) y = math.sqrt( b*b – 4*a*c)/ (2*a);
16
Participation Example (Skip for now.) Write a program to find the 4 th root of a number input by the user. fourthRoot.c
17
Problem (Skip for now) Write a program to find the length of side C of a right triangle, given the lengths of sides A and B. Write the program.
18
4.4 Type Conversions 11/09/15
19
Matt Richtel Tonight 11/09/15 Tonight at 6 PM in the SUB Jordan Ballroom: See Matt Richtel who wrote A Deadly Wondering. Fill out reflection form for 5 extra credit points on homework score. Or read the book and do a report. – Report due December 1 st.
20
New Assignments See the 119 website. Help with: Challenge Activity4.3.2: Tree Height. – TreeHeight = ? Let's have a look at Program 6.
21
Conversion between Numeric Types Conversion in expressions may happen implicitly or be required explicitly Consider Variable is a double. Literal is an int. The int is implicitly converted to a double. Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010
22
Conversion between Numeric Types However, this does not work in the opposite direction In expressions If all values are of same type, result is of that type If one of the values is a floating-point, the result is floating-point Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010
23
Casting Conversion can be forced with type casting What was illegal Can be made legal by preceding the desired type in parentheses This is considered a unary operator Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010
24
Program Example Write a program that asks for the number of cars in a neighborhood and the number of households. It should output the average number of cars per household. Let's write an algorithm first. Can use casting to get an accurate double answer.
25
Questions What is wrong with this statement? How could it be made legal? int i = 1.5; How can I find x 2.5 in Java?
28
Escape sequenceChar \nnewline \ttab \'single quote \"double quote \\backslash
29
Using mobaxterm to Transfer Your Program to onyx Make a directory on onyx to put you program in – mkdir myProg4 Open the directory in the left area of mobaxterm Drag the file from the PC’s folder to myProg4 mobaxterm
30
Java Examples
31
public class Quarterback{ private int age; private int yearPlaying; //Set the qb's age public void setAge(int age){ this.age = age; } //Get the qb's age public int getAge(){ return age; } //Set the number of years playing public void setYearPlaying(int yearPlaying){ this.yearPlaying = yearPlaying; } //Get the number of years playing public int getYearPlaying(){ return yearPlaying; } //Write a method to get the difference between //This Quarterback's age and another one's age public int findAgeDifference(Quarterback qb){ int diff = Math.abs(this.age - qb.age); return diff; } }
32
public class QuarterbackTester{ public static void main(String [] args){ Quarterback rypien = new Quarterback(); Quarterback finley = new Quarterback(); rypien.setAge(19); finley.setAge(20); int difference = rypien.findAgeDifference(finley); System.out.println(difference); }
33
import java.util.Scanner; public class Einstein { public static void main(String[] args) { Scanner kbd = new Scanner(System.in); double e; double m; final double SPEED_OF_LIGHT = 300000000.0; System.out.println("mass?"); m = kbd.nextDouble(); e = m * SPEED_OF_LIGHT * SPEED_OF_LIGHT; System.out.println(e + " newtons"); kbd.close(); }
34
import java.util.Scanner; import java.text.DecimalFormat; public class FindTax { public static void main(String[] args) { //Define a tax rate constant final double TAX_RATE = 0.06; //Input price Scanner scan = new Scanner(System.in); System.out.println("Car Price?"); double price = scan.nextDouble(); //Figure tax double tax = price * TAX_RATE; //Output tax DecimalFormat formatter = new DecimalFormat("$0.00"); System.out.println("Tax is " + formatter.format(tax)); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.