Java: Getting Started Basic Class Structure

Slides:



Advertisements
Similar presentations
1 Classes and Objects in Java Basics of Classes in Java.
Advertisements

Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
Hello AP Computer Science!. What are some of the things that you have used computers for?
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
C# B 1 CSC 298 Writing a C# application. C# B 2 A first C# application // Display Hello, world on the screen public class HelloWorld { public static void.
CompSci 42.1Intro to Java Anatomy of a Class & Terminology Running and Modifying a Program.
Session One Introduction. Personal Introduction Role of programmers Robot Examination HUD & HID Uploading Code.
1 Classes and Objects in C++ 2 Introduction Java is a true OO language and therefore the underlying structure of all Java programs is classes. Anything.
1 Classes and Objects in Java Basics of Classes in Java.
Objective You will be able to define and identify the basic components of a java program by taking notes, seeing examples, and completing a lab. Construction.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
SE-1010 Dr. Mark L. Hornick 1 Java Programming Basics.
Anatomy.1 Anatomy of a Class & Terminology. Anatomy.2 The Plan Go over MoveTest.java from Big Java Basic coding conventions Review with GreeterTest.java.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Indentation & Readability. What does this program do? public class Hello { public static void main ( String[] args ) { //display initial message System.out.println(
Hello Computer Science!. Below is an example of a Hello World program in JAVA. While it is only three lines of code, there are many things that are happening.
CSI 3125, Preliminaries, page 1 Compiling the Program.
ICBT  Basura Ramanayaka  Eshani werapitiya  Hasitha Dananjaya.
Java and C++ Transitioning. A simple example public class HelloWorldApp { public static void main(String[] args) { //Display the string. System.out.println("Hello.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING String and Scanner Objects.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
JAVA METHODS (FUNCTIONS). Why are they called methods? Java is a strictly object-oriented programming language Methods are functions inside of objects.
Execution ways of program References: www. en.wikipedia.org/wiki/Integrated_development_environment  You can execute or run a simple java program with.
COMP 110 More about classes Luv Kohli October 3, 2008 MWF 2-2:50 pm Sitterson 014.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
INTRODUCTION Java is a true OO language the underlying structure of all Java programs is classes. Everything must be encapsulated in a class that defines.
An Introduction to the Java Language App Design Flat Rock Community Schools Introductory Java Programming.
Java Methods and Applications CSIS 3701: Advanced Object Oriented Programming.
Object-Oriented programming for Beginners LEAPS Computing 2015
Topic: Classes and Objects
Test 2 Review Outline.
A Java Program: // Fig. 2.1: Welcome1.java // Text-printing program.
USING ECLIPSE TO CREATE HELLO WORLD
Classes and Objects in Java
Classes and Objects in Java
Classes and Objects in Java
Writing Methods.
An Introduction to Java – Part I, language basics
Tonga Institute of Higher Education
Building Java Programs
Assignment 7 User Defined Classes Part 2
Example with Static Variable
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
CS 180 Assignment 6 Arrays.
class PrintOnetoTen { public static void main(String args[]) {
Building Java Programs
Constructors, GUI’s(Using Swing) and ActionListner
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Chapter 9 Objects and Classes
Developing Java Applications with NetBeans
In this class, we will cover:
Building Java Programs
Introductory Java Programming
Developing Java Applications with NetBeans
Names of variables, functions, classes
Building Java Programs
Methods/Functions.
Chapter 6 Arrays.
ITE “A” GROUP 2 ENCAPSULATION.
Presentation transcript:

Java: Getting Started Basic Class Structure Programming & Data Structures

Everything Starts with a Class public class MyClassName { // some variables; what the class “knows” // some methods; the class's “behaviour” // if this is where your program starts public static void main(String args[ ] ) { // code } Programming & Data Structures

In Eclipse, Classes are found in Packages // MyClassName.java package edu.np.pds.sample; public class MyClassName { // some variables // some methods // if this is where your program starts public static void main(String args[ ]) { // code } Programming & Data Structures

Programming & Data Structures Classes and Objects Classes are “kinds” of things like Employee or Message. Objects are instances of Classes like Andrew Pletch or “Hello, world!”. Objects are always created with the new method. class MyMessage { String contents; public MyMessage(String c) { contents = c; } public String toString() { return “My Message: “ + contents; . . . MyMessage helpMessage = new MyMessage(“Help!!!!”); System.out.println(helpMessage); Programming & Data Structures

Variables are Declared inside of Classes Variables come in two flavours – class variables and instance variables. Class Variables: declared with the reserved word static the value is shared by all instances of the class. Instance Variables: declared without the reserved word static values are specific to a single object. Programming & Data Structures

Methods are Declared inside Classes Methods come in two kinds – class methods and instance methods. Class methods: defined with the reserved word static can be executed with just the class name and not the name of a variable referencing a specific object. Instance methods: defined without the reserved word static can't execute without an actual object exhibiting the behaviour of the method. Programming & Data Structures

Class and Instance Variable Example class MyMessage { static String prompt; String contents; public MyMessage(String c) { contents = c; } static public void setPrompt(String p) { prompt = p; public String toString() { return prompt + contents; . . . MyMessage helloMessage = new MyMessage(“Hello!!!!”); MyMessage.setPrompt(“Message: “); MyMessage goodbyeMessage = new MyMessage (“Good-bye.”); System.out.println(helloMessage); System.out.println(goodbyeMessage); Programming & Data Structures

Programming & Data Structures Useful links http://docstore.mik.ua/orelly/java/exp/ch05_06.htm Programming & Data Structures