242-210 Programming Fundamentals 2: Simple/2 1 242-210 F II Objectives – –give some simple examples of Java applications and one applet 2. Simple Java.

Slides:



Advertisements
Similar presentations
Chapter 2 - Introduction to Java Applications
Advertisements

 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 3 Variables Primitive Data Types calculations & arithmetic operators Readings: Chapter 2.
Dialogs. Displaying Text in a Dialog Box Windows and dialog boxes –Up to this our output has been to the screen –Many Java applications use these to display.
Introduction To Computers and Programming Lecture 2: Your first program Professor: Evan Korth New York University.
1 Introduction to Java Programming Lecture 4 Using JOptionPane Spring 2008.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Introduction to Computers and Programming Lecture 3: Variables and Input Professor: Evan Korth New York University.
Java Intro. A First Java Program //The Hello, World! program in Java public class Hello { public static void main(String[] args) { System.out.println("Hello,
 2003 Prentice Hall, Inc. Modified for use with this class. All rights reserved. 1 Introduction to Computers and Programming in JAVA: V Primitive.
Introduction to Computers and Programming Lecture 4: Mathematical and Relational Operators.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
Chapter 2 - Introduction to Java Applications
Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.4A Simple Java Applet: Drawing a String 3.5Two More Simple.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Computer Science A 1: 3/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 24 - Introduction to Java Applications and Applets Outline 24.1Introduction 24.2Basics of a Typical.
Introduction to Java.
JOptionPane class. Dialog Boxes A dialog box is a small graphical window that displays a message to the user or requests input. A variety of dialog boxes.
From BlueJ to NetBeans SWC 2.semester.
Computer Science [3] Java Programming II - Laboratory Course Lab 6: Introduction to Java Applets Sample Applets from the Java Simple Java Applet: Drawing.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing.
OOP (Java): Simple/ OOP (Java) Objectives – –give some simple examples of Java applications and one applet 2. Simple Java Programs Semester.
1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Jaeki Song ISQS6337 JAVA Lecture 03 Introduction to Java -The First Java Application-
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Input & Output In Java. Input & Output It is very complicated for a computer to show how information is processed. Although a computer is very good at.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
9/6: Variable Types, Arithmetic Operators, Comparison Operators Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic.
Introduction to Java. 2 A Brief History of OOP and Java Early high level languages –_________________________ More recent languages –BASIC, Pascal, C,
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
1 COMP 241: Object-Oriented Programming with Java Fall 2004 Lecture 1 September 27, 2004 Serdar Taşıran.
9/20: The while Repetition Structure last time’s program repetition structures: what they are the while repetition structure homework due on Thursday program.
1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command.
1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.
CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab.
Dialog Boxes.
Data Types – Reference Types Objective To understand what reference types are The need to study reference types To understand Java standard packages To.
GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven.
Overview of Java CSCI 392 Day One. Running C code vs Java code C Source Code C Compiler Object File (machine code) Library Files Linker Executable File.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing.
Data Types – Reference Types Objective To understand what reference types are The need to study reference types To understand Java standard packages To.
Casting, Wrapper Classes, Static Methods, JOptionPane Class.
Object Oriented Programming Object and Classes Lecture 3 MBY.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing.
AP Java 10/1/2015. public class Rolling { public static void main( String [] args) public static void main( String [] args) { int roll; int roll; for.
UFCFY5-30-1Multimedia Studio Coding for Interactive Media Fundamental Concepts.
1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing a Line of Text 2.2.1Compiling and Executing.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Foundations of Programming: Java
A Java Program: // Fig. 2.1: Welcome1.java // Text-printing program.
2.5 Another Java Application: Adding Integers
Chapter 2 - Introduction to Java Applications
Chapter 2 - Introduction to Java Applications
Chapter 24 - Introduction to Java Applications and Applets
MSIS 655 Advanced Business Applications Programming
Chapter 3: Introduction to Objects and Input/Output
Chapter 2 - Introduction to Java Applications
Java Intro.
Object Oriented Programming
Chapter 2 - Introduction to Java Applications
JOptionPane class.
F II 2. Simple Java Programs Objectives
Chapter 2: Java Fundamentals cont’d
Presentation transcript:

Programming Fundamentals 2: Simple/ F II Objectives – –give some simple examples of Java applications and one applet 2. Simple Java Programs Semester 2, Original Slides by Dr. Andrew Davison

Programming Fundamentals 2: Simple/2 2 Contents 1.Steps in Writing a Java Application 2. Hello.java 3.A Better Programming Environment? 4. Comparison.java

Programming Fundamentals 2: Simple/ Steps in Writing a Java Appl. Foo.java text file holding the application javac Foo.java call the Java compiler Foo.class class file holding Java bytecodes java Foo execute the class using the Java runtime system (the JVM)

Programming Fundamentals 2: Simple/ Hello.java import java.io.*; public class Hello { public static void main(String args[]) { System.out.println(“Hello Andrew”); } } // end of class

Programming Fundamentals 2: Simple/2 5 Compile & Run

Programming Fundamentals 2: Simple/2 6 Notes import imports pre-defined classes from the java.io package – –* means “all classes” All Java applications must have a main() function (method) – –static means the method is ‘inside’ the class – –public means the method can be called from outside the class – –args[] stores command line arguments (not used here) continued

Programming Fundamentals 2: Simple/2 7 The Java file (e.g. Hello.java ) must contain a public class with the file’s name (e.g. Hello class). continued

Programming Fundamentals 2: Simple/2 8 System.out is the standard output stream – –like cout (C++) or stdout (C) System.out.println() is the main print function (method) in Java. Hello main() calls System.out.println(…) writes to screen via output stream

Programming Fundamentals 2: Simple/ A Better Programming Environment? When first learning Java, it is best to use a simple programming environment – –it forces you to understand how the language works I write/compile/execute my programs using a simple configuable text editor called Notepad++ – –see continued

Programming Fundamentals 2: Simple/2 10 Useful Notepad++ features – –it will format Java code automatically – –colour-coded display of code – –it is possible to add calls to javac, java, appletviewer to the Notepad++ menu no need to leave the editor to compile/run – –there is an optional window that show the output from running Java code

Programming Fundamentals 2: Simple/2 11

Programming Fundamentals 2: Simple/2 12 Notepad++ Macro Menu Read the Notepad++Java.pdf document at the course Website

Programming Fundamentals 2: Simple/ Comparison.java import javax.swing.JOptionPane; // GUI dialogs public class Comparison { public static void main( String args[] ) { String firstNumber,secondNumber,result; int number1,number2; // read user numbers firstNumber = JOptionPane.showInputDialog( "Enter first integer:"); secondNumber = JOptionPane.showInputDialog( "Enter second integer:" ); :

Programming Fundamentals 2: Simple/2 14 // convert numbers number1 = Integer.parseInt( firstNumber ); number2 = Integer.parseInt( secondNumber ); result = ""; if ( number1 == number2 ) result = number1 + " == " + number2; if ( number1 != number2 ) result = number1 + " != " + number2; if ( number1 number2 ) result = result + "\n" + number1 + " > " + number2; if ( number1 <= number2 ) result = result + "\n" + number1 + " <= " + number2 :

Programming Fundamentals 2: Simple/2 15 if ( number1 >= number2 ) result = result + "\n" + number1 + " >= " + number2; // Display results JOptionPane.showMessageDialog( null, result, "Comparison Results", JOptionPane.INFORMATION_MESSAGE ); } // end of main() } // end of Comparison class

Programming Fundamentals 2: Simple/2 16 Compile & Run $ javac Comparison.java $ java Comparison $

Programming Fundamentals 2: Simple/2 17 Notes The Comparison class is just a single main() function (method) continued

Programming Fundamentals 2: Simple/2 18 showInputDialog() and showMessageDialog() are simple (but quite flexible) methods for reading/writing input in dialog boxes – –defined inside the JOptionPane class continued

Programming Fundamentals 2: Simple/2 19 Notice the use of familiar C/C++ control structures (e.g. if, while ) and data types (e.g. int, double ) “...” + “...” means concatenation (put strings together) String is a pre-defined class for strings. int is a built-in type (just like C’s int).

Programming Fundamentals 2: Simple/2 20 Calling Methods Methods are defined in classes. Syntax for calling a method: Class.method-name or object.method-name – –e.g. JOptionPane.showMessageDialog(...); this calls the showMessageDialog( ) method in the class JOptionPane Classes start with an upper case letter.

Programming Fundamentals 2: Simple/2 21 The Integer Class int is a C-like built-in type Integer is a Java class for integers – –used when integer methods are required Integer.parseInt() converts a string to int

Programming Fundamentals 2: Simple/2 22 Classes as Libraries One way of using a class is as a library for useful methods. One way of using a class is as a library for useful methods. –the JOptionPane class has many methods for creating different kinds of dialog boxes; –the Integer class has many methods for manipulating integers