Declaring console static and global import java.util.*; public class Test { static Scanner console = new Scanner (System.in); public static void main(String[]

Slides:



Advertisements
Similar presentations
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Advertisements

Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Objectives Learn about objects and reference variables Explore how to use predefined methods in a program.
1 Parameter Passing Revisited Overview l Parameter passing l Passing parameters by value l Passing parameters by reference l Some Examples l Preview: Data.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
***** SWTJC STEM ***** Chapter 3-1 cg 36 Java Class Libraries & API’s A class library is a set of classes that supports the development of programs. Java.
Chapter 2-3: Basic Elements of Java. Chapter Objectives Discover how to use arithmetic operators. Examine how a program evaluates arithmetic expressions.
1 Scanner objects. 2 Interactive programs We have written programs that print console output. It is also possible to read input from the console.  The.
Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer.
©Silberschatz, Korth and Sudarshan1 Methods Method Basics Parameters Void vs. Non-void Methods Recursion.
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
Java Program Components Java Keywords - Java has special keywords that have meaning in Java. - You have already seen a fair amount of keywords. Examples.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 5.
The String Class A String is an object. An object is defined by a class. In general, we instantiate a class like this: String myString = new String(“Crazy.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
Mixing integer and floating point numbers in an arithmetic operation.
Java 1.5 The New Java Mike Orsega Central Carolina CC.
Java Arrays and Methods MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh.
Lecture 2 Objectives Learn about objects and reference variables.
String and Scanner CS 21a: Introduction to Computing I First Semester,
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
CHAPTER 5 GC 101 Input & Output 1. INTERACTIVE PROGRAMS  We have written programs that print console output, but it is also possible to read input from.
COSC236 Modularity1 Top-Down Design Design before code most programs so far have been simple, less than one page in length most problems are not so simple.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
Java and C++ Transitioning. A simple example public class HelloWorldApp { public static void main(String[] args) { //Display the string. System.out.println("Hello.
Building java programs, chapter 3 Parameters, Methods and Objects.
Review :chapters What is an algorithm? A step by step description of how to accomplish a task. For example, Adding 3 numbers N1, N2 and N3 Add.
 CSC111 Quick Revision. Problem Write a java code that read a string, then show a list of options to the user to select from them, where:  L to print.
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Using Classes and Objects We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: object creation and.
Object Oriented Programming Lecture 2: BallWorld.
Introduction to programming in java
UFCFY5-30-1Multimedia Studio Coding for Interactive Media Fundamental Concepts.
Introduction to Computer Science What is Computer Science? Getting Started Programming.
Chapter 7: User-Defined Methods J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Third Edition Third.
Java Memory Management
Chapter 2 Clarifications
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Java Memory Management
CSC1401 Input and Output (and we’ll do a bit more on class creation)
AKA the birth, life, and death of variables.
Arrays in Classes and Methods
using System; namespace Demo01 { class Program
Chapter 2 Elementary Programming
Yanal Alahmad Java Workshop Yanal Alahmad
Maha AlSaif Maryam AlQattan
Data types, Expressions and assignment, Input from User
SELECTION STATEMENTS (1)
INPUT STATEMENTS GC 201.
Java so far Week 7.
Chapter 3: Introduction to Objects and Input/Output
CS110D Programming Language I
AKA the birth, life, and death of variables.
class PrintOnetoTen { public static void main(String args[]) {
Introduction to Java Brief history of Java Sample Java Program
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Module 4 Loops and Repetition 4/7/2019 CSE 1321 Module 4.
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
More on iterations using
Presentation transcript:

Declaring console static and global import java.util.*; public class Test { static Scanner console = new Scanner (System.in); public static void main(String[] args) { System.out.println("Enter name"); String name = console.next(); System.out.println("Hello" + name); }

import java.lang – happens automatically – includes String, Math, wrapper classes import java.util.*; - necessary for Scanner

Public static methods All methods of Math are public and static General use of a Math method is Math.methodName (params) Or Math.pow(2,3) If you import the class statically, you can use the method directly import static java.lang.Math.*; pow(2,3)

Formal parameter – a variable declared in the method heading Actual parameter – a variable or expression listed in a method call

COSC236 Modularity5 public class Demo { public static void main(String[] args) { // Part III - strings String s = "Java is fun!"; System.out.println(s); // print it (7) sMethod(s); System.out.println(s); // print it (9) } public static void sMethod(String sTest) { sTest = sTest.substring(8, 11); // change it System.out.println(sTest); // print it (8) } Java is fun! fun Java is fun!

precaution with strings Remember - the String class is immutable String str; str = "hello"; str 1500  "hello" str = "Hello There"; str 1800  "Hello There" anytime you use assignment with a String variable, new memory space is allocated also, the String class has no methods that allow you to change an existing string COSC236 Modularity6

7 public class DemoPassByReference { public static void main(String[] args) { // Part II - objects and object references StringBuffer sb = new StringBuffer("Hello, world"); System.out.println(sb); // print it (4) sbMethod(sb); System.out.println(sb); // print it (6) System.out.println(" "); } public static void sbMethod(StringBuffer sTest) { sTest = sTest.insert(7,"cruel "); // change it System.out.println(sTest); // print it (8) } Hello, world Hello, cruel world

Wrapper classes – Integer, Double, etc are also immutable See example7-11 on p. 402