210 likes | 316 Views
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
E N D
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 • So how do we solve a problem? • Take 3 main steps • Understand the problem • Make a plan • Implement the plan © 2007 William Albritton
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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