String Output ICS 111 Introduction to Computer Science

  • Slides: 21
Download presentation
String Output • ICS 111: Introduction to Computer Science I – William Albritton •

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 12/12/2021 © 2007 William Albritton 1

Problem Solving • • Computer science is basically about problem solving So how do

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

Terminology • • Like any other discipline, computer science has its own “fancy words”

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 • • • 12/12/2021 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 3

Writing Algorithms • When writing an algorithm, you need to follow a few rules

Writing Algorithms • When writing an algorithm, you need to follow a few rules 1. The algorithm must be a detailed series of steps that will solve the program 2. 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) 3. The algorithm should be simple, unambiguous, and correct 12/12/2021 © 2007 William Albritton 4

Class Exercise 1 • Critique the following algorithm – Algorithm to make spam musubi

Class Exercise 1 • Critique the following algorithm – Algorithm to make spam musubi 1. 2. 3. 4. 5. 6. 12/12/2021 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 5

Solving a Computer Problem • Computer problem – How can we get a computer

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 1. Understand the problem 2. Make an algorithm 3. Implement the algorithm by editing, compiling, and running a computer program 12/12/2021 © 2007 William Albritton 6

3 Steps for Computer Problem 1. Understand the problem 2. Make an algorithm –

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

Terminology • Edit a program with an editor – Write program in Java language

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 • 12/12/2021 Creates screen output © 2007 William Albritton 8

Terminology • Program – A set of instructions for a computer – Also called

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 • 12/12/2021 For example, Program. java © 2007 William Albritton 9

Terminology • Java language (Program. java) – Machine-independent, high-level computer language • • Easy

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 • 12/12/2021 For example, an IBM computer only understands IBM machine code © 2007 William Albritton 10

Software Installation • See the ICS 111 class webpage for instructions on how to

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 – j. GRASP (Graphical Representations of Algorithms, Structures and Processes) • 12/12/2021 An IDE (Integrated Development Environment) used to edit, compile, and run programs © 2007 William Albritton 11

Java Language Syntax • Similar to human languages, Java also has syntax or grammar

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 Program. Name{ public static void main(String[] args){ //program code } } 12/12/2021 © 2007 William Albritton 12

Program Name & File Name • Syntax for a basic Java computer program public

Program Name & File Name • Syntax for a basic Java computer program public class Program. Name{ public static void main(String[] args){ //Write your program code here! } } – Don’t worry about what all these words mean yet • • • 12/12/2021 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 Program. Name © 2007 William Albritton 13

Terminology • String – A sequence of characters between double quotes • "Aloha!" is

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 12/12/2021 © 2007 William Albritton 14

Class Exercise 2 • Write a Java program that outputs a greeting in each

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 My. Program{ public static void main(String[] args){ System. out. println("Hey, y'all!"); } } 12/12/2021 © 2007 William Albritton 15

Meaning of class • Meaning of each word on the first line of a

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

Meaning of main • Meaning of each word of the main method • public

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

A Messy Program • This is how not to write a program! – See

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

Java Coding Standard • In ICS 111, we have a Java Coding Standard that

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 • • 12/12/2021 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 19

Terminology • Comments – Notes about the program for the reader – Ignored by

Terminology • Comments – Notes about the program for the reader – Ignored by the computer • • • 12/12/2021 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 20

Types of Comments • • Single-line comments // Multi-line comments – Begin with /*

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 • 12/12/2021 Press the “Generate Documentation” icon (an open book) in j. GRASP to see this © 2007 William Albritton 21