CS 180 Recitation 8 / {30, 31} / 2007. Announcements Project 1 is out –Due 10:00pm –Start early! –Use the newsgroup for your questions –LWSN B146.

Slides:



Advertisements
Similar presentations
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Ch2: Getting Started with Java - Objectives After.
Advertisements

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Program development.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to Computer Programming Stringing Along – Using Character and String Data.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Structure of.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Structure of.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java.
Strings as objects Strings are objects. Each String is an instance of the class String They can be constructed thus: String s = new String("Hi mom!");
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
Java Coding David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey.
***** SWTJC STEM ***** Chapter 3-1 cg 36 Java Class Libraries & API’s A class library is a set of classes that supports the development of programs. Java.
Computer Programming Lab(5).
Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer.
Integer Division What is x? int x = 1729 / 100; Answer: 17.
Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)
Recitation 2 Main Method, API & Packages, Java Basics.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Getting Started with Java Recitation – 1/23/2009 CS 180 Department of Computer Science, Purdue University.
COMP String and Console I/O Yi Hong May 18, 2015.
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Input/Output in Java. Output To output to the command line, we use either System.out.print () or System.out.println() or System.out.printf() Examples.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
CS1101: Programming Methodology Aaron Tan.
Introduction to Programming
Java 1.5 The New Java Mike Orsega Central Carolina CC.
String and Scanner CS 21a: Introduction to Computing I First Semester,
Chapter 3 Introduction To Java. OBJECTIVES Packages & Libraries Statements Comments Bytecode, compiler, interpreter Outputting print() & println() Formatting.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
Chapter 2 Getting Started with Java. Objectives After you have read and studied this chapter, you should be able to Identify the basic components of Java.
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Java’s String Class.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CHAPTER 5 GC 101 Input & Output 1. INTERACTIVE PROGRAMS  We have written programs that print console output, but it is also possible to read input from.
Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)
Java Variables, Types, and Math Getting Started Complete and Turn in Address/Poem.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC Programming I Lecture 9 September 11, 2002.
CSCI 51 Introduction to Computer Science Joshua Stough February 3, 2009.
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.
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING String and Scanner Objects.
Strings and I/O. Lotsa String Stuff… There are close to 50 methods defined in the String class. We will introduce several of them here: charAt, substring,
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
بسم الله الرحمن الرحيم CPCS203: Programming II. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr.
C OMMON M ISTAKES CSC Java Program Structure  String Methods.
Copyright © Curt Hill Simple I/O Input and Output using the System and Scanner Objects.
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.
SUMMARY OF CHAPTER 2: JAVA FUNDAMENTS STARTING OUT WITH JAVA: OBJECTS Parts of a Java Program.
(Dreaded) Quiz 2 Next Monday.
Introduction to programming in java
Yanal Alahmad Java Workshop Yanal Alahmad
CS116 OBJECT ORIENTED PROGRAMMING II LECTURE 1 Part II
Something about Java Introduction to Problem Solving and Programming 1.
INPUT STATEMENTS GC 201.
Program Style Console Input and Output
CS 177 Week 3 Recitation Slides
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
Introduction to Classes and Methods
Java Variables, Types, and Math Getting Started
MSIS 655 Advanced Business Applications Programming
Introduction to Java Programming
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Introduction to Java Applications
Strings CSE 1310 – Introduction to Computers and Programming
Getting Started with Java
More on iterations using
Presentation transcript:

CS 180 Recitation 8 / {30, 31} / 2007

Announcements Project 1 is out –Due 10:00pm –Start early! –Use the newsgroup for your questions –LWSN B146 Lab help hours 7-10pm Monday through Wednesday

Naming Conventions Methods and variables begin with lower case int age, aMultipleWordVariable, another_multiple_word_variable; public void method() { } public void aMultipleWordMethod() { } Classes should begin with upper case public class MyClass { } Names should be descriptive for clarity and understanding No identifier can start with number Java is case sensitive aGe, Age, agE, AgE are all different in Java

Coding Conventions Code should be organized in a clean and clear fashion so that it is easy to read Tabs and whitespace do not change your code –Use them to cleanly space out your code –Don’t leave too much white space around – find a balance (ex: don’t double space lines of code)

Bad Code (My eyes! My eyes!) public class veRybAdpRogramMINgSTYLe { String NONdESCrIPTVariaBL_Ename; public String GEtS____tuFF () { return NONdESCrIPTVariaBL_Ename; } }

Good Code (Ah… much better!) public class Name { String name; public String getName() { return name; }

Comments Used to document your code Help remind you what variables or methods do/represent (a good name helps as well) Comment classes, class variables, and methods to describe what they are and what they do // a single line comment can use double slashes /* or you can do the multi-line style as well */ /* multi-line comments look like this */ /** * javascript comments look like this paramName our parameter */

String A Java class (we will discuss classes and objects in more detail later) A sequence of 0 or more characters including the 26 letters, digits 0-9, punctuation marks, and whitespaces Declaration – can use constructor String str = new String(“I love my TA!!!”); String str = “I love my TA!!!”; Use length() method to determine number of characters in the string String str = “I love my TA!!!”;// str.length() returns 15 Indexing characters starts at 0 –Starting indexing at 0 will be seen again when we study arrays) –Therefore, the characters are at indices 0…str.length()-1

String Substring – method used to grab a portion of the string –First index – the beginning character –Last index – the character after the last taken one (if none specified, go to end of string) String str = “myString!!!”; String str2 = str.substring(0,str.length()); // “myString!!!” String str3 = str.substring(3,5); // “tr” String str4 = str.substring(3); // “tring!!!”

Other Common Methods int indexOf(char) –Returns the first index of the character argument in the String, -1 if the character does not exist in the String char charAt(int) –Returns the character at the integer argument index in the String; an exception is thrown if the index is invalid Concatenation (+) –Combine two Strings together Many more methods – consult API

Dates Date class –From java.util package –Use toString method to print out the date toString is implicitly called for you SimpleDateFormat class –From java.text package –Used to format dates SimpleDateFormat sdf = new SimpleDateFormat(“MM/dd/yy”);

User Input Scanner –A java provided class in the java.util library –Many uses Input and parsing –Keyboard –Files (later in the semester) –Strings JOptionPane –A java provided class in the javax.swing library –Provides many methods to generate windows Messages Input

Two Sample Programs import java.util.Scanner; public class SampleProgram { public static void main(String[] args) { Scanner input = new Scanner (System.in); System.out.print(“Enter a word: “); String word = input.next(); System.out.println(“Your word is “ + word.length() + “ letters long.”); } import javax.swing.JOptionPane; public class SampleProgram { public static void main(String[] args) { String word = JOptionPane.showInputDialog(null, “Enter a word:”); JOptionPane.showMessageDialog(null, “Your word is “ + word.length() + “ letters long.”); }

Pig Latin! import java.util.Scanner; public class PigLatinTranslator { public static void main(String[] args) { PigLatinTranslator plt = new PigLatinTranslator(); Scanner input = new Scanner (System.in); System.out.print(“Enter a word to translate: “); String word = input.next(); System.out.println(word + “in pig latin is “ + plt.translate() + “.”); } // a very naïve translator public String translate(String s) { return s.substring(1) + s.charAt(0) + “ay”; }