1 / 21

String Output

String Output . ICS 111: Introduction to Computer Science I William Albritton Information and Computer Sciences Department at the University of Hawai ‘ i at M ā noa. © 2007 William Albritton. Problem Solving . Computer science is basically about problem solving

orrick
Download Presentation

String Output

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. String Output • ICS 111: Introduction to Computer Science I • William Albritton • Information and Computer Sciences Department at the University of Hawai‘i at Mānoa © 2007 William Albritton

  2. Problem Solving • Computer science is basically about problem solving • So how do we solve a problem? • Take 3 main steps • Understand the problem • Make a plan • Implement the plan © 2007 William Albritton

  3. Terminology • Like any other discipline, computer science has its own “fancy words” (jargon) • Computer scientists use the word algorithm for the plan that is used to solve a problem • Example algorithms from daily life • Taking the train from Makuhari to Nikko • Going scuba diving at 5 Caves, Maui • Teaching a friend to surf at Baby Queens © 2007 William Albritton

  4. Writing Algorithms • When writing an algorithm, you need to follow a few rules • The algorithm must be a detailed series of steps that will solve the program • The algorithm can be written in many different ways • English (or Japanese, Chinese, Tagalog, etc.) • Drawings (diagrams, pictures, etc.) • Pseudocode (mix of English & a computer language) • The algorithm should be simple, unambiguous, and correct © 2007 William Albritton

  5. Class Exercise 1 • Critique the following algorithm • Algorithm to make spam musubi • Put rice in rice cooker • Cook the rice • Mash rice into ball • Put nori (seaweed) on top of rice ball • Cook spam on stove • Put spam on rice ball © 2007 William Albritton

  6. Solving a Computer Problem • Computer problem • How can we get a computer to output a message to the screen? • So how do we solve a problem using the computer? • Take 3 main steps • Understand the problem • Make an algorithm • Implement the algorithm by editing, compiling, and running a computer program © 2007 William Albritton

  7. 3 Steps for Computer Problem • Understand the problem • Make an algorithm • Output the message “Hello, World” • Implement the algorithm by editing, compiling, and running a computer program • See Program.java © 2007 William Albritton

  8. Terminology • Edit a program with an editor • Write program in Java language & store on computer • Creates Program.java (text file) • Compile a program with a compiler • Check for errors & create Java bytecode • Creates Program.class (bytecode file) • Run (execute) a program with an interpreter • Translate each bytecode to machine code & implement machine code • Creates screen output © 2007 William Albritton

  9. Terminology • Program • A set of instructions for a computer • Also called “application” • Code • Contents of a computer program • Text file • A file of characters (text, or alphanumeric letters) that is used to store the computer program • Java programs end with *.java • For example, Program.java © 2007 William Albritton

  10. Terminology • Java language (Program.java) • Machine-independent,high-level computer language • Easy for humans to write & understand • Java bytecode (Program.class) • Machine-independent, low-level language • Java bytecode can be executed on any computer • Machine code (created by interpreter) • Machine-dependent, low-level language • For example, an IBM computer only understands IBM machine code © 2007 William Albritton

  11. Software Installation • See the ICS 111 class webpage for instructions on how to install the software • JDK (Java Development Kit) • Used to compile programs • Also includes JRE (Java Runtime Environment) which is the interpreter used to run programs • jGRASP (Graphical Representations of Algorithms, Structures and Processes) • An IDE (Integrated Development Environment) used to edit, compile, and run programs © 2007 William Albritton

  12. Java Language Syntax • Similar to human languages, Java also has syntax or grammar rules to follow • If we don’t follow these rules, the compiler will tell us • Syntax for a basic Java computer program • If you write your program in a different way, it may have syntax errors & not compile public class ProgramName{ public static void main(String[] args){ //program code } } © 2007 William Albritton

  13. Program Name & File Name • Syntax for a basic Java computer program public class ProgramName{ public static void main(String[] args){ //Write your program code here! } } • Don’t worry about what all these words mean yet • We will learn later what public, class, [], static, void, args (arguments) mean • Just know that your program code goes inside the two inner curly brackets, i.e. "{" & "}" • Also, the file name must match the ProgramName © 2007 William Albritton

  14. Terminology • String • A sequence of characters between double quotes • "Aloha!" is a string • "!@#$%^&*()_+" is a string • "this is a string" is a string • In order to output a string to the computer screen, you need to use the following code, and put your string inside the parentheses • System.out.println("Hey, y'all!"); • Also, don’t forget the semicolon at the end of every line of code © 2007 William Albritton

  15. Class Exercise 2 • Write a Java program that outputs a greeting in each language that each person of your group speaks • Question: What is the name of your file that you will use to store your program? public class MyProgram{ public static void main(String[] args){ System.out.println("Hey, y'all!"); } } © 2007 William Albritton

  16. Meaning of class • Meaning of each word on the first line of a program • public class ProgramName • public: this class can be used outside of its package (folder) • class: a class is the basic building block of the Java language & can be used as a blueprint to create objects • ProgramName: this is the name of your program & the name of your file © 2006 William Albritton

  17. Meaning of main • Meaning of each word of the main method • public static void main(String[] args) • public: other classes can use this method • static: this is a class method, so method call has format ClassName.methodName() • void: this method does not return anything • main: name of this special kind of method which is used to start the program • String[]: an array of Strings • args: the parameter of this method, which contains each word entered on the command line, stored as an array of Strings © 2006 William Albritton

  18. A Messy Program • This is how not to write a program! • See thisisonemessyprogram.java • Pressing the “Generate CSD” button in jGRASP will help to fix this • The “Control Structure Diagram” automatically indents your code • Also shows the structure of your code © 2007 William Albritton

  19. Java Coding Standard • In ICS 111, we have a Java Coding Standard that should be followed when writing programs • The main purpose is to make sure the programs are neat and easy to understand • See the “links” column of the class webpage • Again, don’t freak out if you don’t understand what everything means yet © 2007 William Albritton

  20. Terminology • Comments • Notes about the program for the reader • Ignored by the computer • Your program should read like a book! • Other people should be able to easily understand it • You should be able to easily understand it, so you can use it later to review for the exams © 2007 William Albritton

  21. Types of Comments • Single-line comments // • Multi-line comments • Begin with /* and end with */ • Javadoc comments • A way to comment your code, so that it can be automatically documented • See Java Coding Standard on webpage under “links” • Important as code increases in size & complexity • Easy for other folks to understand your code • Press the “Generate Documentation” icon (an open book) in jGRASP to see this © 2007 William Albritton

More Related