java programming
play

Java Programming Pancakes sequence of In one bowl instructions - PowerPoint PPT Presentation

8/20/15 Hello Hello World World What is a program? n Definition: a Java Programming Pancakes sequence of In one bowl instructions telling 1 cup flour CS 160, Fall Semester 2012 CS 160, Fall Semester 2012 a computer what mix:


  1. 8/20/15 Hello Hello World World What is a program? n Definition: a Java Programming Pancakes sequence of In one bowl instructions telling 1 ½ cup flour CS 160, Fall Semester 2012 CS 160, Fall Semester 2012 a computer what mix: TOPICS 1 tbsp sugar to do 1 tsp salt • Computer Programs 1 tsp baking • Using Eclipse n Analogy: cooking • Hello World Program powder • Program Components recipes 1 CS 160, Fall Semester 2015 2 Hello Hello World World Programs as Recipes Programs ≠ Recipes At some point the analogy breaks down: Recipes specify n n Recipes are read by people What ingredients to use 1. n People can make inferences. What to do with them 2. n If something goes wrong, people react. Sometimes conditional: “bake until golden brown”, “salt n n People sometimes make mistakes. to taste” Programs specify n n Programs are read by machines What information (data) to use 1. n Machines do exactly what they are told! What operations to apply 2. n No matter how badly things go wrong. Sometimes conditional: “while file is not empty”, “for n n But, they never make mistakes or get tired. every element in array” CS 160, Fall Semester 2015 3 CS 160, Fall Semester 2015 4 1

  2. 8/20/15 Hello Hello World World Definitions Object-oriented programming n program n Java programs use objects and methods n a set of directions telling a computer exactly what to do n An ‘object’ is a collection of code and data that you n programming languages treat as a unit. n precise languages for specifying sequences of n Objects have ‘methods’ – code that implements directions to a computer actions that apply to the object. n unlike English -- no ambiguities! no nuances! n Objects have ‘members’ – data that is associated n algorithm with the object. n a sequence of steps to be followed to solve a problem n A Java cooking program might have a line like n independent of any programming language ‘egg.scramble()’ to scramble an egg … CS 160, Fall Semester 2015 5 CS 160, Fall Semester 2015 6 Hello Hello What ¡does ¡eclipse ¡look ¡like? ¡ Using ¡Eclipse ¡ World World n OK, ¡the ¡intro ¡screen ¡isn’t ¡too ¡helpful ¡ n Feel ¡free ¡to ¡hit ¡the ¡‘X’ ¡by ¡welcome ¡ n That ¡will ¡kill ¡the ¡intro ¡screen ¡ n Create ¡a ¡new ¡project ¡using ¡the ¡menus ¡ n File ¡-­‑> ¡New ¡-­‑> ¡Project ¡ n Select ¡a ¡Java ¡project ¡ n Give ¡it ¡a ¡name/directory ¡ n Then ¡create ¡a ¡new ¡class ¡ n File ¡-­‑> ¡New ¡-­‑> ¡Class ¡ n It ¡will ¡offer ¡to ¡make ¡stubs, ¡etc. ¡ n Let ¡it ¡make ¡a ¡main ¡stub ¡for ¡you… ¡ CS 160, Fall Semester 2015 7 CS 160, Fall Semester 2015 8 2

  3. 8/20/15 Hello Hello World Hello World Program HelloWorld ¡via ¡Eclipse ¡ World // HelloWorld // Author: Chris Wilcox // Date: 1/1/2015 // Class: CS160 // Email: wilcox@cs.colostate.edu import java.lang.*; public class HelloWorld { public static void main(String[] args) { System. out .println( "Hello World!" ); } } CS 160, Fall Semester 2015 9 CS 160, Fall Semester 2015 10 Hello Hello Comments ¡ Import directive World World § Any ¡line ¡beginning ¡with ¡// ¡or ¡any ¡text ¡ § Tells ¡Java ¡what ¡other ¡classes ¡or ¡packages ¡ between ¡/* ¡and ¡*/. ¡ (collecUons ¡of ¡classes) ¡to ¡use. ¡ § Describes ¡the ¡behavior ¡and ¡a\ributes ¡of ¡a ¡ § Java.lang.* ¡includes ¡the ¡class ¡ System , ¡always ¡ program. ¡ assumed ¡by ¡compiler. ¡ § For ¡humans ¡only ¡– ¡Java ¡compiler ¡ignores ¡all ¡ § Must ¡be ¡above ¡the ¡class ¡declaraUon, ¡at ¡the ¡ comments ¡when ¡generaUng ¡code. ¡ top ¡of ¡the ¡program. ¡ § But ¡really ¡important ¡for ¡humans! ¡ § Eclipse ¡will ¡automaUcally ¡create ¡import ¡ direcUves ¡as ¡needed. ¡ CS 160, Fall Semester 2015 11 CS 160, Fall Semester 2015 12 3

  4. 8/20/15 Hello Hello Class ¡Heading ¡ Curly ¡brackets ¡{ ¡and ¡} ¡ World World § Creates ¡a ¡new ¡class ¡that ¡can ¡contain ¡code ¡ § Used ¡to ¡group ¡statements ¡together ¡into ¡ (methods) ¡and ¡data: ¡ larger ¡units ¡ § ¡this ¡one ¡is ¡called ¡HelloWorld ¡ § Everything ¡within ¡the ¡outer ¡brackets ¡is ¡part ¡of ¡ the ¡class ¡ HelloWorld ¡ § ¡Every ¡Java ¡file ¡begins ¡with ¡a ¡class ¡heading, ¡ which ¡is ¡always ¡public. ¡ § Everything ¡within ¡the ¡inner ¡brackets ¡is ¡part ¡of ¡ the ¡method ¡ main ¡ § ¡Class ¡name ¡must ¡match ¡file ¡name ¡ § ¡this ¡file ¡is ¡called ¡HelloWorld.java ¡ § ¡Only ¡one ¡class ¡per ¡file ¡is ¡allowed ¡ CS 160, Fall Semester 2015 13 CS 160, Fall Semester 2015 14 Hello Hello Class ¡Body ¡ Main ¡Method ¡ World World § Everything ¡between ¡{ ¡and ¡} ¡of ¡the ¡class ¡header ¡ § The ¡main ¡funcUon ¡is ¡the ¡acUon ¡taken ¡when ¡a ¡ is ¡the ¡class ¡body. ¡ user ¡invokes ¡this ¡class ¡from ¡the ¡command ¡line. ¡ § The ¡class ¡body ¡defines ¡the ¡data ¡in ¡the ¡object ¡ § Or ¡when ¡run ¡from ¡Eclipse ¡or ¡a ¡program ¡icon ¡ and ¡its ¡methods. ¡ on ¡the ¡desktop. ¡ § This ¡class ¡has ¡no ¡data, ¡and ¡only ¡one ¡method, ¡ § The ¡program ¡prints ¡ Hello ¡World! ¡to ¡the ¡screen ¡ which ¡is ¡the ¡program ¡starUng ¡point. ¡ or ¡terminal. ¡ ¡ § How ¡do ¡console ¡programs ¡and ¡graphical ¡ programs ¡differ? ¡ CS 160, Fall Semester 2015 15 CS 160, Fall Semester 2015 16 4

  5. 8/20/15 Hello Hello Main ¡Components ¡ Main ¡Body ¡ World World § Return ¡Value: ¡ § The ¡method ¡body ¡is ¡a ¡sequence ¡of ¡steps ¡or ¡ Java ¡statements ¡ § What ¡the ¡method ¡computes ¡ § In ¡this ¡case, ¡nothing, ¡so ¡ void ¡ § Statements ¡are ¡separated ¡by ¡semicolons, ¡ cannot ¡be ¡omi\ed! ¡ § Method ¡Name: ¡ § In ¡this ¡case, ¡the ¡method ¡has ¡just ¡one ¡ § main ¡is ¡reserved ¡for ¡the ¡top-­‑level ¡acUon ¡ statement ¡ § Names ¡should ¡ ¡describe ¡acUons ¡ § Arguments: ¡ § Calls ¡the ¡system ¡object ¡to ¡output ¡a ¡line ¡of ¡text ¡ ¡ containing ¡ Hello ¡World! ¡ § Specify ¡the ¡input ¡to ¡a ¡method ¡ § In ¡this ¡case, ¡command ¡line ¡arguments ¡ CS 160, Fall Semester 2015 17 CS 160, Fall Semester 2015 18 Hello PracUcum ¡ World § Use ¡Eclipse ¡to ¡create ¡a ¡workspace, ¡project, ¡ and ¡write ¡a ¡program ¡ CS 160, Fall Semester 2015 19 5

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend