CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.

Slides:



Advertisements
Similar presentations
CSCI S-1 Section 7. Coming Soon Problem Set Three, Part B – Tuesday, July 14, 17:00 EST Problem Set Four (72 + 5/15 points) – Friday, July 17, 17:00 EST.
Advertisements

Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
Reading Information From the User Making your Programs Interactive.
JAVA Control Statement.
Computer Programming Lab(4).
1 Scanner objects. 2 Interactive programs We have written programs that print console output. It is also possible to read input from the console.  The.
1 BUILDING JAVA PROGRAMS CHAPTER 3 THE SCANNER CLASS AND USER INPUT.
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Conditional If Week 3. Lecture outcomes Boolean operators – == (equal ) – OR (||) – AND (&&) If statements User input vs command line arguments.
Programming Methodology (1). import java.util.*; public class FindCost3 { public static void main(String[] args ) { Scanner sc = new Scanner(System.in);
Lecture 2: Classes and Objects, using Scanner and String.
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
Chapter 5 Loops.
Chapter 2 Elementary Programming
1 Fencepost loops “How do you build a fence?”. 2 The fencepost problem Problem: Write a class named PrintNumbers that reads in an integer called max and.
CSCI S-1 Section 6. Coming Soon Homework Part A – Friday, July 10, 17:00 EST Homework Part B – Tuesday, July 14, 17:00 EST Mid-Term Quiz Review – Friday,
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014 Office hours: Thursday 1300 – 1400hrs.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
CSci 111 – computer Science I Fall 2014 Cynthia Zickos WRITING A SIMPLE PROGRAM IN JAVA.
1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops; Procedural Design reading: 5.1 – 5.2; 4.5.
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Chapter 4: Control Structures II
COMP Flow of Control: Branching 1 Yi Hong May 19, 2015.
CSC1401 Strings (text). Learning Goals Working with Strings as a data type (a class) Input and output of Strings String operations.
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSE 110 Review Session Hans Hovanitz, Kate Kincade, and Ian Nall.
Announcements Quiz 1 Next Monday. int : Integer Range of Typically –2,147,483,648 to 2,147,483,647 (machine and compiler dependent) float : Real Number.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
Building Java Programs Program Logic and Indefinite Loops.
CSCI S-1 Section 4. Deadlines for Homework 2 Problems 1-8 in Parts C and D – Friday, July 3, 17:00 EST Parts E and F – Tuesday, July 7, 17:00 EST.
 CSC111 Quick Revision. Problem Write a java code that read a string, then show a list of options to the user to select from them, where:  L to print.
Java Scanner Class Keyboard Class. User Interaction So far when we created a program there was no human interaction Our programs just simply showed one.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement.
CSCI S-1 Section 8. Coming Soon Problem Set Four (72 + 5/15 points) – Tuesday, July 21, 17:00 EST.
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Interactive Programs Programs that get input from the user 1 In PowerPoint, click on the speaker icon then click the "Play" button to hear the narration.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
(Dreaded) Quiz 2 Next Monday.
Sophomore Scholars Java
CompSci 230 S Programming Techniques
Chapter 2 Clarifications
CSC111 Quick Revision.
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
TemperatureConversion
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Compiling and Running a Java Program
Chapter 6 More Conditionals and Loops
Maha AlSaif Maryam AlQattan
Computer Programming Methodology Input and While Loop
TK1114 Computer Programming
SELECTION STATEMENTS (1)
Something about Java Introduction to Problem Solving and Programming 1.
Introduction to Classes and Methods
Know for Quiz Everything through Last Week and Lab 7
class PrintOnetoTen { public static void main(String args[]) {
Differences between Java and JavaScript
Module 3 Selection Structures 4/5/2019 CSE 1321 Module 3.
Repetition Statements
Building Java Programs
Building Java Programs
Building Java Programs
Presentation transcript:

CSCI S-1 Section 5

Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from lecture – cp ~libs1/pub/src/lectures/leitner/July6*.java.

The Scanner Class A java library to take user input Not included automatically – Unlike lang.System, for System.out.print() Need to tell the compiler where the library is So IMPORT the library at the very top of the program file – Import java.util.scanner; // includes definition for scanner only – Import java.util.*// includes all definitions in util library

Declaring Scanner Objects Scanner scan = new Scanner(System.in); – “new” creates an instance (or “object”) of the java class Scanner Now we can use predefined methods from the scanner class – Scan.nextInt();// get an int – Scan.nextDouble();// get a double – Scan.next() or scan.nextLine();// get a string – Scan.nextShort();// get a short int – Scan.nextByte();// get a byte

Example 1 - Nint // take an int from user and print it back out Class Nint { public static void main (String[] args) { }

Example 1 - Nint // take an int from user and print it back out import java.util.*; Class Nint { public static void main (String[] args) { System.out.print(“Please enter a number: ”) Scanner scan= new Scanner(System.in); int n= scan.nextInt(); System.out.printl(“You have entered the number ” + N + “.”); }

Example 2 - Celsius // take a double (representing Celsius) and change it to a double for Fahrenheit Class Celsius { public static void main (String[] args) { }

Example 2 - Celsius // take a double (representing Celsius) and change it to a double for Fahrenheit import java.util.*; Class Celsius { public static void main (String[] args) { System.out.print(“Please enter the temperature in Celsius outside:“ ) Scanner scan= new Scanner(System.in); double n= scan.nextDouble(); System.out.printl(“You current temperature in Fahrenheit is “ + (n*(9.0/5) + 32) + “.”); }

Example 3 - Born // Take a String and print it back out class Born { public static void main(String[] args) { }

Example 3 - Born // Take a String and print it back out import java.util.*; class Born { public static void main(String[] args) { System.out.print("Where were you born? "); Scanner scan = new Scanner(System.in); String s = scan.nextLine(); System.out.println("You were born in " + s + "! Wow, so was I!"); }

Example 4 - Bad // Don’t try this at home class Bad { public static void main(String[] args) { System.out.print("I need some vital input here: "); Scanner scan = new Scanner(System.in); String n = scan.nextLine(); System.out.println("Wow, " + n + " was some vital input!"); }

Conditional Statements Use if-else conditions to create ‘branches’. Conditions evaluate to true or false. int x=3; if (x < 5) System.out.println("This is True"); if (x >= 5) System.out.println("This is False"); // alternatively if (x < 5) System.out.println("This is True"); else System.out.println("This is False");

Example 5 – Conditions Inside Loops // print odd digits up to N, as entered by user. print * for even digits class Condition1 { public static void main(String[] args) { }

Example 5 – Conditions Inside Loops // print odd digits up to N, as entered by user. Print * for even digits import java.util.*; class Condition1 { public static void main(String[] args) { System.out.print("Enter a number. I am going to count odds: "); Scanner scan = new Scanner(System.in); int stop = scan.nextInt(); for (int i = 1; i <= stop; i++) if (i % 2 == 1) System.out.println(i); else System.out.println("*"); }

Example 6 – Smallest // picks out the smallest number of ten numbers entered (all ints > 0) class Smallest { public static void main(String[] args) { }

Example 6 – Smallest // picks out the smallest number of ten numbers entered (all ints > 0) import java.util.*; class Smallest { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int smallest = 0; for (int i = 1; i <= 10; i++) { System.out.println("Enter a number greater than 0: "); int n = scan.nextInt(); if (n < smallest || smallest == 0) smallest = n; } System.out.println("The smallest number was " + smallest + "."); }