Lecture 2: Classes and Objects, using Scanner and String.

Slides:



Advertisements
Similar presentations
Lecture 1: Comments, Variables, Assignment. Definitions The formal (human-readable) instructions that we give to the computer is called source code The.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
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.
1 Control Structures (and user input). 2 Flow of Control The order statements are executed is called flow of control By default, statements in a method.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Chapter 2 storing numbers and creating objects Pages in Horstmann.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
Copyright 2013 by Pearson Education Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading:
COMP 110 Spring Announcements Computers in class on Friday: Lab Office Hours: Monday 12-2 New students see me after class Administrative Changes.
COMP 110: Introduction to Programming Tyler Johnson January 14, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Lecture 2: Static Methods, if statements, homework uploader.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
The Java Programming Language
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
CSE 131 Computer Science 1 Module 1: (basics of Java)
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Lecture 9 Using Objects. Remember: 3 Different Kinds of Classes 1.Application Class – what we've been doing – Has public static void main ( String []
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
CSC Programming I Lecture 6 September 4, 2002.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
CS101: Introduction to Computer Science Slides adapted from Sedgewick and Wayne Copyright © Your First Java.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
OOP Basics Classes & Methods (c) IDMS/SQL News
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
Unit 1 Review By: Mr. Jacobs.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Chapter 1 Object Orientation: Objects and Classes.
Copyright 2010 by Pearson Education APCS Building Java Programs Chapter 1 Lecture 1-1: Introduction; Basic Java Programs reading:
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
Chapter 2 Clarifications
Introduction to Java part 2
Elementary Programming
Building Java Programs
Primitive Data, Variables, Loops (Maybe)
Data types, Expressions and assignment, Input from User
BIT115: Introduction to Programming
Control Statement Examples
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Introduction to Java part 2
Building Java Programs
Lec 4: while loop and do-while loop
Building Java Programs Chapter 2
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Building Java Programs
Building Java Programs
Introduction to Java part 2
Building Java Programs
LCC 6310 Computation as an Expressive Medium
Unit 3: Variables in Java
Building Java Programs Chapter 2
Building Java Programs
Lecture 9 Using Objects.
Presentation transcript:

Lecture 2: Classes and Objects, using Scanner and String

Recap Two categories of data types: primitives and classes Examples of primitives? – int (whole numbers) – double (numbers with decimal points) – boolean (true and false) Examples of classes? – Math(uses static methods to calculate) VERY DIFFERENT FROM – String (a sequence of characters, i.e. text) – Scanner (used to get input from the user or a file)

"Instantiable" Classes With Math we use the class itself to access the static methods like Math.random() or Math.sqrt() An instantiable class (String, Scanner) is like a template or pattern from which any number of objects can be created – Allows storing more complex data than primitives – Uses object-oriented patterns to create and modify

More "Instantiable" Classes Objects of Instantiable Classes are more complex than primitives – Primitive values are just pieces of data (numbers, characters, or true/false) For example, 2.3 is a value of type double – Instances of classes, on the other hand, contain both data and ways of acting on that data For example, a String object holds a series of letters (like "the answer is ") but also comes with a way to UPPERCASE the letters or extract parts of the String into other Strings Google String javadoc to get the docdoc Let's see write an example that shows Strings in action

Demo time, Create project StringDemo String message = new String ("to err is human, "); String reply = new String("but not for computers!"); System.out.println( message ); System.out.println( message.toUpperCase() ); System.out.println( message + reply ); System.out.println( message + reply.replace('o','x'); double result = 53.4; System.out.println("the answer is " + result);

Some Class related Terminology An instance of a class is called an object – in the example: message, reply are objects of String To make a new object, use the new operator The actions you can invoke on an object are called its methods – in the example: toUpper(), replace() are String methods To use a method, we use dot notation as in the example To find out the details of how to use a particular class, we use the javadoc documentationjavadoc documentation

Reading input from console An example of a very useful class (Scanner) How do we get input from the user in Java? – Answer: The Scanner class is one way. – Scanner is described in Section of text – or check Scanner javadocScanner

Example //Since Scanner is in package java.util, //import statement. import java.util.*; /* * This program takes an int from the user and prints * out one bigger. */ public class ScannerExample { public static void main(String[] args) { Scanner input = new Scanner(System.in); int i = input.nextInt(); i = i + 1; System.out.println(i); } }

Packages There are so many different classes that they are organized into different packages – To use a class from some package, you use an import statement on the top of your source file Classes in this code fragment have been underlinedthis code fragment – We don't need to import System and String because they are in package java.lang, which consists of classes so commonly used that it is always imported automatically

Libraries In most programming languages, it takes a lot of work just to do some very basic commonly used tasks Solution: Just write it once, and make it standard and available to everyone This bunch of pre-written code is called a library The Java libraries are enormous! and still growing...

Compile-time errors, logic errors, run-time errors Human error occurs constantly when programming; we distinguish three main types in this course: – Compile-time errors (includes syntax errors) This means your source code isn't ``grammatically correct'' These errors are always caught by the compiler Often due to typos, or not understanding valid syntax Sytem.out.println("Hello world!"); Usually easiest to correct

– Run-time error Caught by the JVM, but only when that bad something actually happens (like dividing by zero) Often not too hard to correct once it happens public class HelloWorld3 { public static void main(String[] args) { int i = 0; i = i / i; //This causes a run time error (division by zero) System.out.println(i); } }

– Logic error (aka semantic error) The ``everything else'' category It runs fine, but not the way you want it to Usually causes your program to behave in a strange, unexpected way (see example and the fix)examplefix Usually due to an error in your logic when writing the program Using a debugger can be helpful to figure it out System.out.println("Hello word!"); Usually the hardest to correct

Lab 3 Create Lab3 project in BlueJ We experiment with a home made Calculator – using Scanner to read input numbers – using if to do the right calculation – using String to print result with a mesage Turn in procedure. When finished... – Create Lab3.jar – Click "Homework Uploader" link – Enter passcode, estimated score, browse to Lab3.jar – then click upload