+ All Categories
Home > Documents > Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory...

Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory...

Date post: 18-Jan-2016
Category:
Upload: millicent-dean
View: 220 times
Download: 0 times
Share this document with a friend
Popular Tags:
34
Chapter 4 10/26 & 10/27
Transcript
Page 1: 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 4

10/26 & 10/27

Page 2: Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.

More Linux Commands

mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file

Page 3: 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

Page 4: Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.

4.1 Named Constants

Page 5: Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.

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 = ?

Page 6: Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.

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.

Page 7: Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.

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

.

Page 8: Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.

4.3 Using Math Methods

Page 9: Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.

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

Page 10: Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.

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

Page 11: Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.

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

Page 12: Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.

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

Page 13: Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.

Standard Mathematical Methods

Call to method Math.sqrt

Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010

Page 14: Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.

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

Page 15: Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.

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);

Page 16: Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.

Participation Example(Skip for now.)

•Write a program to find the 4th root of a number input by the user.•fourthRoot.c

Page 17: Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.

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.

Page 18: Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.

4.4 Type Conversions

11/09/15

Page 19: Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.

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.

Page 20: Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.

New Assignments

See the 119 website. Help with: Challenge Activity4.3.2:

Tree Height.

– TreeHeight = ? Let's have a look at Program 6.

Page 21: Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.

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

Page 22: Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.

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

Page 23: Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.

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

Page 24: Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.

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.

Page 25: Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.

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?

Page 26: Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.
Page 27: Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.
Page 28: Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.

Escape sequence Char

\n newline

\t tab

\' single quote

\" double quote

\\ backslash

Page 29: Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.

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

Page 30: Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.

Java Examples

Page 31: Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.

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;}

}

Page 32: Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.

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);

}}

Page 33: Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.

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();

}

}

Page 34: Chapter 4 10/26 & 10/27. More Linux Commands mkdir rmdir echo > redirect output mv file, directory mv oldFileName newFileName more file rm file.

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));

}

}


Recommended