Lecture 8 Using casts, Strings and WordUtil. Agenda Generating random numbers Casts – Casting a double into an int – Casting an int into a char – Casting.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
1 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Introduction to Programming
Input review If review Common Errors in Selection Statement Logical Operators switch Statements Generating Random Numbers practice.
The ArrayList Class and the enum Keyword
What is output line of the following C++ code? Please trace int i = 1, QP; DATA: 4, B, 3, A double credit, totCredit=0.0; double num = 0.0; string LG;
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Computer Programming Lab(7).
Picture It Very Basic Game Picture Pepper. Original Game import java.util.Scanner; public class Game { public static void main() { Scanner scan=new Scanner(System.in);
Q1 Review. Other News US News top CS Universities global-universities/computer- science?int=994b08.
CS110 Programming Language I
Building Java Programs
Computer Programming Lab 8.
CS324e - Elements of Graphics and Visualization A Little Java A Little Python.
Lec 12 Writing an Instantiable Class. Agenda Review objects and classes Making our own class to create objects – Our first "Instantiable" class – with.
Today’s topics: I/O (Input/Output). Scribbler Inputs & Outputs  What are the Scribbler’s inputs and outputs?  reset button  motors/wheel  light sensor.
Lec 7 Using Static Class Methods. Today We work with static utility methods of a class We learn how JavaDocs can help us figure out how to use them.
Building Java Programs
Introduction to Computers and Programming Lecture 12: Math.random() Professor: Evan Korth New York University.
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.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
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.
Computer Programming Lab(5).
Applications in Java Towson University *Ref:
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 4 part 3 GEORGE KOUTSOGIANNAKIS.
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
The String Class A String is an object. An object is defined by a class. In general, we instantiate a class like this: String myString = new String(“Crazy.
Characters. Character Data char data type – Represents one character – char literals indicated with ' '
Lecture 9 Using Objects. Remember: 3 Different Kinds of Classes 1.Application Class – what we've been doing – Has public static void main ( String []
Math Class Part of the Java.lang package. This package is from object so you do not have to import the lang package. Static: Math Class is static.
Introduction to Computer Programming Math Random Dice.
CS1101: Programming Methodology Aaron Tan.
Using Java Class Library
String and Scanner CS 21a: Introduction to Computing I First Semester,
The Math class Java provides certain math functions for us. The Math class contains methods and constants that can be very useful. The Math class is like.
1 Chap. 3 Creating Objects The String class Java Class Library (Packages) Math.random() Reading for this Lecture: L&L, 3.1 – 3.6.
Creating and Using Class Methods. Definition Class Object.
1 Advanced Programming Examples Output. Show the exact output produced by the following code segment. char[,] pic = new char[6,6]; for (int i = 0; i
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
© 2007 Lawrenceville Press Slide 1 Chapter 4 Review Assignment Statement An assignment statement gives a value to a variable. Assignment can take several.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
AP Java 10/1/2015. public class Rolling { public static void main( String [] args) public static void main( String [] args) { int roll; int roll; for.
Strings CSE 1310 – Introduction to Computers and Programming
Strings CSE 1310 – Introduction to Computers and Programming
Categories of loops definite loop: Executes a known number of times.
Software Development I/O and Numbers
using System; namespace Demo01 { class Program
Computer Programming Methodology File Input
An Introduction to Java – Part I
מבוא למדעי המחשב, סמסטר א', תשע"א תרגול מס' 2
Introduction to Java Programming
בתרגול הקודם אתר הקורס (הודעות, פרטי סגל הקורס, עבודות, פורום, מערכת הגשת תרגילים וכו') שימוש בחלון ה-command, פקודות בסיסות קוד Java: הידור (= קומפילציה)
class PrintOnetoTen { public static void main(String args[]) {
Building Java Programs
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Building Java Programs
In this class, we will cover:
CSE 142, Spring 2013 Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 5.1 – 5.2.
Lecture 9 Using Objects.
Building Java Programs
PROGRAMMING ASSIGNMENT I
More on iterations using
Computer Science Club 1st November 2019.
Agenda Warmup Lesson 2.4 (String concatenation, primitive types, etc)
Presentation transcript:

Lecture 8 Using casts, Strings and WordUtil

Agenda Generating random numbers Casts – Casting a double into an int – Casting an int into a char – Casting a char into an int Strings again – reading a String – checking the length of a String – can we cast a String into an int? – Or an int to a String? Using the WordUtil class

Generating Random Numbers Recall Lab2 the Math.random() method: – double myNumber; – myNumber = Math.random(); // (from 0 to.999) Then in Lab5: – double secret; – secret = Math.round(Math.random()*100); 0 to 100 ( 0 and 100 come up half as often as others) – OR secret = (int)( Math.random()*100); 0 to 99 ( evenly distributed)

A Cast "filters" a value into a different type int x = (int) ; // x gets 4 double z = 56.23; int y = (int) z; // y gets 56, z remains z = (double) y; // z gets 56.0 char letter = 'c'; int code = (int) letter; // code gets 97 letter = (char) 65; // letter gets 'A' letter = 'A'; // this is more clear, same value

For this lab We'll use casts to convert a random double to int – int num = (int)( Math.random()*100); 0 to 99 ( evenly distributed)

String class Create a String: – String word; – word = "hello"; Read a String – word = in.next(); // in is a Scanner object Compare 2 strings – if ( word.equals("hello")) {...println("good morning"); }

How long is a String Print the length: –...println("length of word = " + word.length() ); Check if the length is 5 letters: – if ( word.length() == 5 )

Remember the 3 Different Kinds of Classes ? 1.Application Class – what we've been doing – Has public static void main ( String [] args) 2.Instantiable Class – use to make objects from – String, Scanner 3.Classes with only static utility methods – like Math class

WordUtilWordUtil class – another class with static utility methods How to get the 15 th word? How to check if a word is in the dictionary?