csci s 1 section 8
Download
Skip this Video
Download Presentation
CSCI S-1 Section 8

Loading in 2 Seconds...

play fullscreen
1 / 25

CSCI S-1 Section 8 - PowerPoint PPT Presentation


  • 77 Views
  • Uploaded on

CSCI S-1 Section 8. Coming Soon. Problem Set Four (72 + 5/15 points) Tuesday, July 21, 17:00 EST. Why Isn’t This Nice?. public static void notSoNiceDice { System.out.println ("Your turn!"); int roll1 = ( int ) ( Math.random () * 6) + 1;

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' CSCI S-1 Section 8' - lada


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
coming soon
Coming Soon
  • Problem Set Four (72 + 5/15 points)
    • Tuesday, July 21, 17:00 EST
why isn t this nice
Why Isn’t This Nice?

public static void notSoNiceDice {

System.out.println("Your turn!");

introll1 = (int) (Math.random() * 6) + 1;

System.out.println("You rolled a " + roll1 + ".");

introll2 = (int) (Math.random() * 6) + 1;

System.out.println("Then you rolled a " + roll2 + "!");

System.out.println("The product of your rolls was " + (roll1 * roll2));

System.out.println("My turn!");

introll3 = (int) (Math.random() * 6) + 1;

System.out.println("I rolled a " + roll3 + ".");

int roll4 = (int) (Math.random() * 6) + 1;

System.out.println("Then I rolled a " + roll4 + "!");

System.out.println("The sum of my rolls was " + (roll3 + roll4));

}

it s repetitive repetitive repetitive
It’s Repetitive, Repetitive, Repetitive!!!

Four times we evaluate (int) (Math.random() * 6) + 1.

Why might we want to make this a separate rollDie() method?

  • Easier to read: if method calls rollDie(), you know what\'s going on
  • Easier to change: modify and test a single method
  • Easier to reuse : call rollDie() from many board games!
the familiar void
The Familiar Void

//void methods have “side effects” and can help modularize

Public static void main (String[] args) {

methodTwo()

}

public static void methodTwo() {

methodOne();

}

public static void methodOne() {

System.out.println("Inside method one");

}

non void methods return values
Non-Void Methods “Return” Values

public static void what?

To get results back from rollDie(), have it return a value

Why can’t we just use global/class variables?

  • anything can access or change them
  • it can be hard to determine which method made the change

The semantics of ‘return’ are not mysterious

  • int n = 3+4 //+ returns 7
  • int n = sum(3,4) //sum returns 7
i nt rolldie
introllDie()

public static introllDie() { //return an int in the range 1-6 incl.

}

int rolldie
introllDie()

public static introllDie() { //return an int in the range 1-6 incl.

inti = (int) (Math.random()*6 + 1);

return i;

}

public static introllDie() { //same, but in one line

}

int rolldie1
introllDie()

public static introllDie() { //return an int in the range 1-6 incl.

inti = (int) (Math.random()*6 + 1);

return i;

}

public static introllDie() { //same, but in one line

return (int) (Math.random()*6 + 1);

}

going to town
Going to Town

//some exciting things you can do with return values

public static void main (String[] args) {

System.out.println(rollDie()); //print it

int answer = rollDie(); //assign it

intsumOfTwoRolls = rollDie() + rollDie(); //evaluate it

double rollsOverFive = rollDie() / 5.0; //slice and dice it

}

parameter passing
Parameter Passing

public static booleanisGeneralissimoFrancoStillDead() {

return true;

}

what methods don’t always return the same value?

  • depend on user/keyboard input
  • depend on random expressions
  • depend on passed parameters
parameter passing1
Parameter Passing

//method to determine if a character is a digit

Public static booleanisDigit(char ch) {

//code to calculate and return goes here

}

//invoking method supplies value for ch

//what does boolean mean here?

booleanb = isDigit(‘2); //sample call

isdigit
isDigit

/**

* Determines if the given character is a digit (betw `0\' and `9\').

* @paramch character to be tested

* @return whether the character is a digit

*/

public static booleanisDigit(char ch) {

}

isdigit1
isDigit

/**

* Determines if the given character is a digit (betw `0\' and `9\').

* @paramch character to be tested

* @return whether the character is a digit

*/

public static booleanisDigit(char ch) {

if ((ch >= `0\') && (ch <= `9\')) return true;

else return false;

}

isdigit concise
isDigit concise

//repeating the code from the last slide…

public static booleanisDigit(char ch) {

if ((ch >= `0\') && (ch <= `9\')) return true;

else return false;

}

public static booleanisDigit(char ch) {

}

isdigit concise1
isDigit concise

//repeating the code from the last slide…

public static booleanisDigit(char ch) {

if ((ch >= `0\') && (ch <= `9\')) return true;

else return false;

}

public static booleanisDigit(char ch) {

return ((ch >= `0\') && (ch <= `9\'));

}

isevendigit
isEvenDigit

/**

* Determines if the given character is an even digit

* @paramch character to be tested

* @return whether the character is an even digit

*/

public static booleanisEvenDigit(char ch) {

}

isevendigit1
isEvenDigit

/**

* Determines if the given character is an even digit

* @paramch character to be tested

* @return whether the character is an even digit

*/

public static booleanisEvenDigit(char ch) {

return ((ch == `0\') || (ch == `2\') || (ch == `4\')

|| (ch == `6\') || (ch == `8\'));

}

isevendigit with indexof
isEvenDigit with indexOf

/**

* Determines if the given character is an even digit

* @paramch character to be tested

* @return whether the character is an even digit

*/

public static booleanisEvenDigit(char ch) {

// string method indexOfreturns -1 when char not in string

}

isevendigit with indexof1
isEvenDigit with indexOf

/**

* Determines if the given character is an even digit

* @paramch character to be tested

* @return whether the character is an even digit

*/

public static booleanisEvenDigit(char ch) {

// string method indexOf returns -1 when char not in string

final String EVENDIGITS = "02468";

return (EVENDIGITS.indexOf(ch) != -1);

}

isodddigit
isOddDigit

//determines if the given character is an odd digit

//define in terms of isDigit and isEvenDigit

public static booleanisOddDigit(char ch) {

}

isodddigit1
isOddDigit

//determines if the given character is an odd digit

//define in terms of isDigit and isEvenDigit

public static booleanisOddDigit(char ch) {

return (isDigit(ch) && !isEvenDigit(ch));

}

example
Example

//read String from keyboard and print out even digits in reverse

public static void main(String[] args) {

}

example1
Example

//read String from keyboard and print out even digits in reverse

import java.util.*;

public static void main(String[] args) {

Scanner kbd = new Scanner(System.in);

String input = kbd.nextLine();

for (inti = input.length() - 1; i >= 0; i--)

if (isEvenDigit(input.charAt(i))

System.out.print(input.charAt(i));

System.out.println();

}

summary
Summary

Familiar method calls with parameters

System.out print( <some string> );

charAt( <some number> );

Declare a method that takes parameters by putting the data type and variable name inside the parentheses

public static double fahrenheitToKelvin(double fahr) {}

Call a method that takes parameters by putting something with the right data type into the parentheses

d = fahrenheitToKelvin(32.0);

ad