Upload
millicent-dean
View
223
Download
0
Tags:
Embed Size (px)
Citation preview
Chapter 4
10/26 & 10/27
More Linux Commands
mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file
Chapter TopicsConstantsStandard Mathematical MethodsCasting
4.1 Named Constants
ConstantsUnnamed 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 = ?
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.
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
.
4.3 Using Math Methods
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
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) – log10
x
Math.exp(x) – ex
Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010
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
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
Standard Mathematical Methods
Call to method Math.sqrt
Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010
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
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);
Participation Example(Skip for now.)
•Write a program to find the 4th root of a number input by the user.•fourthRoot.c
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.
4.4 Type Conversions
11/09/15
Matt Richtel Tonight11/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 1st.
New Assignments
See the 119 website. Help with: Challenge Activity4.3.2:
Tree Height.
– TreeHeight = ? Let's have a look at Program 6.
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
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
CastingConversion can be forced with type castingWhat was illegal
Can be made legal
by preceding the desired type in parenthesesThis is considered a unary operator
Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010
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.
Questions
What is wrong with this statement? How could it be made legal?
int i = 1.5;
How can I find x2.5 in Java?
Escape sequence Char
\n newline
\t tab
\' single quote
\" double quote
\\ backslash
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
Java Examples
public class Quarterback{private int age;private int yearPlaying;
//Set the qb's agepublic void setAge(int age){this.age = age;}
//Get the qb's agepublic int getAge(){return age;}
//Set the number of years playingpublic void setYearPlaying(int yearPlaying){this.yearPlaying = yearPlaying;}
//Get the number of years playingpublic int getYearPlaying(){return yearPlaying;}
//Write a method to get the difference between //This Quarterback's age and another one's agepublic int findAgeDifference(Quarterback qb){int diff = Math.abs(this.age - qb.age);return diff;}
}
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);
}}
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();
}
}
import java.util.Scanner;import java.text.DecimalFormat;
public class FindTax {
public static void main(String[] args) {//Define a tax rate constantfinal double TAX_RATE = 0.06;//Input priceScanner scan = new Scanner(System.in);System.out.println("Car Price?");double price = scan.nextDouble();
//Figure taxdouble tax = price * TAX_RATE;//Output tax
DecimalFormat formatter = new DecimalFormat("$0.00");
System.out.println("Tax is " + formatter.format(tax));
}
}