An Overview of JAVA From basic to slightly less basic Jonathan-Lee Jones.

Slides:



Advertisements
Similar presentations
Date: Subject:Distributed Data Processing Name:Maria Br ü ckner.
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Introduction to arrays Data in economy size packages.
Methods Liang, Chapter 4. What is a method? A method is a way of running an ‘encapsulated’ series of commands. System.out.println(“ Whazzup ”); JOptionPane.showMessageDialog(null,
Classes, methods, and conditional statements We’re past the basics. These are the roots.
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Hello, world! Dissect HelloWorld.java Compile it Run it.
An Overview of JAVA From basic to slightly less basic Jonathan-Lee Jones.
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
IB Computer Science II Paul Bui
Programming in Java; Instructor:Moorthy Introduction, Objects, Classes, Libraries1 Programming in Java Introduction.
EMB1006 JAVA Part 2 Arrays and onwards Jonathan-Lee Jones.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Hello AP Computer Science!. What are some of the things that you have used computers for?
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Session One Introduction. Personal Introduction Role of programmers Robot Examination HUD & HID Uploading Code.
1.1 Your First Program Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · October.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.
© 2012 Pearson Education, Inc. All rights reserved. 1-1 Why Java? Needed program portability – Program written in a language that would run on various.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
Java Classes. Consider this simplistic class public class ProjInfo {ProjInfo() {System.out.println("This program computes factorial of number"); System.out.println("passed.
Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.
Java Arrays and Methods MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Building Java Programs Chapter 3 Lecture 3-1: Parameters reading: 3.1.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
CS101: Introduction to Computer Science Slides adapted from Sedgewick and Wayne Copyright © Your First Java.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
Methods. Methods also known as functions or procedures. Methods are a way of capturing a sequence of computational steps into a reusable unit. Methods.
CSI 3125, Preliminaries, page 1 Compiling the Program.
Title Slid CSC 444 Java Programming Arrays By Ralph B. Bisland, Jr.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
2.1 Functions. Functions in Mathematics f x y z f (x, y, z) Domain Range.
By Mr. Muhammad Pervez Akhtar
© 2012 Pearson Education, Inc. All rights reserved types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
1 Arrays and Methods Java always passes arguments by value – that is a copy of the value is made in the called method and this is modified in the method.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Lecture 6: Methods MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture, you will learn… What a method is Why we use.
CS 177 Recitation Week 1 – Intro to Java. Questions?
1 Simple Input Interactive Input - The Class Scanner n Command-Line Arguments.
Execution ways of program References: www. en.wikipedia.org/wiki/Integrated_development_environment  You can execute or run a simple java program with.
Object Oriented Programming Lecture 2: BallWorld.
Introduction to java (class and object). Programming languages: –Easier to understand than CPU instructions –Needs to be translated for the CPU to understand.
Functions + Overloading + Scope
Lecture 1b- Introduction
The need for Programming Languages
Chapter 7 User-Defined Methods.
Java Applet Programming Barry Sosinsky Valda Hilley
User-Defined Functions
An Introduction to Java – Part I, language basics
Building Java Programs
Building Java Programs
Arrays in Java.
IB Computer Science II Paul Bui
Building Java Programs
Presentation transcript:

An Overview of JAVA From basic to slightly less basic Jonathan-Lee Jones

Overview Why JAVA? The VERY basics Data Types Arrays Functions & Methods and more advanced stuff Examples

Why JAVA? The programs that we are writing are very similar to their counterparts in several other languages, so our choice of language is not crucial. We use Java because it is widely available, embraces a full set of modern abstractions, and has a variety of automatic checks for mistakes in programs, so it is suitable for learning to program. There is no perfect language, and you certainly will be programming in other languages in the future.

The VERY Basics Programming in Java. We break the process of programming in Java into three steps: Create the program by typing it into a text editor and saving it to a file named, say, MyProgram.java. Compile it by typing "javac MyProgram.java" in the terminal window. Run (or execute) it by typing "java MyProgram" in the terminal window. The first step creates the program; the second translates it into a language more suitable for machine execution (and puts the result in a file named MyProgram.class); the third actually runs the program.

The VERY Basics public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); }

The VERY Basics class UseArgument { public static void main(String[] args) { System.out.print("Hello, "); System.out.print(args[0]); System.out.print(", "); System.out.print(args[1]); System.out.print(", "); System.out.print(args[2]); System.out.println("!"); }

The VERY Basics class UseArgument { public static void main(String[] args) { if(args.length>0){ System.out.print("Hello, "); System.out.print(args[0]); System.out.print(", "); System.out.print(args[1]); System.out.print(", "); System.out.print(args[2]); System.out.println("!"); System.out.println } Else System.out.println(“Error!”); }

Data Types There are many built in types in JAVA. These built in types can be primitive types, or object types. There are more options available as to what the programmer can do with the more complicated objects than the simple built in primitive types. intcharlong IntegerStringdouble floatDoubleboolean

Arrays Arrays can be though of as a set of values. They can be of any data type (and can even be of user made objects, or other arrays). You represent an array by using the square brackets [] after the data type, and is created using the new command and giving it a size. Eg. int[] numbers = new int[10]; An array is used when you want to store a series of values. The most common array you have all used already! Public static void main(String[] args) This line, in all of your programs contains the array args. It is an array of type String, and is used to store all the command line arguments input by the user.

Arrays When you create a new array using the “new” command, you initialise all the values in it to 0. An array is a series of memory locations, and the variable name points the first position. In JAVA, you can then index each position in the array by using Array[i] where I is the position you want to read. In C++ this is trickier, as you need to know the bit-size of each element to move through memory. If you use “System.out.println(ArrayName);” then you will print the pointer to the memory address the array starts at. Array[0] Array[1] Array[2] Array[3] Array[4] Array

Arrays This code will create an array of numbers between 0 and n, where n is specified by the user. In JAVA arrays start at 0, so the first value would be array[0]. public class createArray { public static void main(final String[] args) { final int n = Integer.parseInt(args[0]); int[]numbers = new int[n] for(int i=0; i<n; ++i) numbers[i] = I; }

Functions & Methods It is often easier to write functions to do a certain task. For example, if you know you are going to require a certain type of calculation repeatedly. Functions are usually of the type static, and methods dynamic, but apart from this they are very similar. public class Max2 { public static int max(final int a, final int b) { if (a>b) return a; else return b; } public static void main(final String[] args) { final int a = Integer.parseInt(args[0]); final int b = Integer.parseInt(args[1]); System.out.println("The maximum of " + a + " and " + b + " is " + max(a,b) + "."); }

Functions & Methods Functions can also be overloaded. This means that you can have a different function (in this case max) with the same name, but taking different inputs. JAVA identifies the correct one to use at compile time. public class Max4 { public static int max(final int a, final int b) { if (a>b) return a; else return b; } public static int max(final int a, final int b, final int c) { return max(max(a,b),c); } public static int max(final int a, final int b, final int c, final int d) { return max(max(a,b,c),d); } public static void main(final String[] args) { final int a = Integer.parseInt(args[0]); final int b = Integer.parseInt(args[1]); final int c = Integer.parseInt(args[2]); final int d = Integer.parseInt(args[3]); StdOut.printf("The maximum of %d, %d, %d, %d is %d.\n", a,b,c,d,max(a,b,c,d)); }

Functions & Methods The term methods usually refers to dynamically called functions. These are not determined at compile time, but are called implicitly on an object. For example this.minVal() would run the minVal method on this object. Methods are often used when the user creates objects of their own. A good example of this is a block object

Functions & Methods class Block{ private double height; private double width; private double length; public Block(double height, double width, double length) { this.height=height; this.width=width; this.length=length; } public double getTopArea() //legth x width { return length*width;} public double getFrontArea() //length x height {return length*height;} public double getSideArea() //width x height {return width*height;} public double getVolume() //area x height {return length*height*width;} }

Functions & Methods class BlockTest{ public static void main(String[] args){ if(args.length<3) { System.err.println(“Not enough arguments”); return; } else { double L = Double.parseDouble(args[0]); double W = Double.parseDouble(args[0]); double H = Double.parseDouble(args[0]); Block testB = new Block(L,W,H); System.out.println(“Volume = “ + testB.getVolume()); }

Thank You