Week5Ch4Intro - Chapter 4 Conditions and Loops Janaka...

Info icon This preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Janaka Balasooriya CSE 110, Arizona State University Chapter 4 Conditions and Loops
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Quick Recap – Chapters 1, 2, and 3 Problem solving process and algorithm development Basic elements in C++ Data types • Arithmetic operations • Data variables and memory allocation • Compound statements ( +=, -= …) Operator precedence rules • Objects More about input/output • Standard input/output Formatting
Image of page 2
Janaka Balasooriya CSE 110, Arizona State University Program Execution Dice Program (Partial) Structure of the program – Flow Chart Statement 1 Statement n Statement 2 - - - import java.util.Random; import java.util.Scanner; public class Dice { public static void main (String[] args) { Random ranGenerator = new Random(); Scanner keyboard = new Scanner(System.in); int dieOneSides, dieTwoSides, dieOne, dieTwo,sumOne=0, sumTwo=0; System.out.println("How many sides does die 1 have?"); dieOneSides=keyboard.nextInt(); System.out.println("How many sides does die 2 have?"); dieTwoSides=keyboard.nextInt(); dieOne = ranGenerator.nextInt(dieOneSides) + 1; System.out.println("Die 1 first roll = " + dieOne); sumOne += dieOne; dieTwo = ranGenerator.nextInt(dieTwoSides) + 1; System.out.println("Die 2 first roll = " + dieTwo); sumTwo += dieTwo; ……………………………………… dieTwo = ranGenerator.nextInt(dieTwoSides) + 1; System.out.println("Die 2 third roll = " + dieTwo); sumTwo += dieTwo; System.out.println("Die 1 rolled a total of " + sumOne + " and rolled " + sumOne/3.0 + "on average"); System.out.println("Die 2 rolled a total of " + sumTwo + " and rolled " + sumTwo/3 .0+ "on average"); } }
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Janaka Balasooriya CSE 100, Arizona State University Need to make decisions during the execution of the program Problem to be solved (Sum): Develop a program that read integers from the keyboard. Then, add
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern