Introduction to Programming with Java Presented by.

Slides:



Advertisements
Similar presentations
Introduction to Eclipse. Start Eclipse Click and then click Eclipse from the menu: Or open a shell and type eclipse after the prompt.
Advertisements

INTRODUCTION Chapter 1 1. Java CPSC 1100 University of Tennessee at Chattanooga 2  Difference between Visual Logic & Java  Lots  Visual Logic Flowcharts.
Using Eclipse. Getting Started There are three ways to create a Java project: 1:Select File > New > Project, 2 Select the arrow of the button in the upper.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Lab#1 (14/3/1431h) Introduction To java programming cs425
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Shlomo Hershkop1 Introduction to java Class 1 Fall 2003 Shlomo Hershkop.
How to Create a Java program CS115 Fall George Koutsogiannakis.
Java Integrated Development Environments: ECLIPSE Part1 Installation.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Introduction to Java CS 331. Introduction Present the syntax of Java Introduce the Java API Demonstrate how to build –stand-alone Java programs –Java.
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Introduction to Java Programming Language Junji Zhi University of Toronto 1.
A Short Introduction to JAVA
Lecture 1: Overview of Java. What is java? Developed by Sun Microsystems (James Gosling) A general-purpose object-oriented language Based on C/C++ Designed.
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
Java versus C# An introduction to C# and Visual Studio.
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
A First Program Using C#
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
FRC Java for Robotics Introduction Team 1279 ColdFusion January 8, 2011.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
Session One Introduction. Personal Introduction Role of programmers Robot Examination HUD & HID Uploading Code.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Lecture 2: Classes and Objects, using Scanner and String.
CSE 131 Computer Science 1 Module 1: (basics of Java)
CS591x A very brief introduction to Java. Java Developed by Sun Microsystems was intended a language for embedded applications became a general purpose.
Introduction to Eclipse CSC 216 Lecture 3 Ed Gehringer Using (with permission) slides developed by— Dwight Deugo Nesa Matic
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.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
CS 112 Department of Computer Science George Mason University CS 112 Department of Computer Science George Mason University Final Review Lecture 14.
Applied Computing Technology Laboratory QuickStart C# Learning to Program in C# Amy Roberge & John Linehan November 7, 2005.
9/2/ CS171 -Math & Computer Science Department at Emory University.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
EIE375 BlueJ: Getting Started Dr Lawrence Cheung.
Java development environment and Review of Java. Eclipse TM Intergrated Development Environment (IDE) Running Eclipse: Warning: Never check the “Use this.
Applications Development
Introduction to Java Java Translation Program Structure
Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
Presented by Lee Zenke 2015 Java Programming PT. 1.
Today… “Hello World” ritual. Brief History of Java & How Java Works. Introduction to Java class structure. But first, next slide shows Java is No. 1 programming.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
Classes, Interfaces and Packages
Execution ways of program References: www. en.wikipedia.org/wiki/Integrated_development_environment  You can execute or run a simple java program with.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Today Javadoc. Packages and static import. Viewing API source code. Upcoming Topics: –protected access modifier –Using the debugger in Eclipse –JUnit testing.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Foundations of Programming: Java
Information and Computer Sciences University of Hawaii, Manoa
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
JAVA MULTIPLE CHOICE QUESTION.
Intro to ETEC Java.
2.5 Another Java Application: Adding Integers
Primitive Data, Variables, Loops (Maybe)
Important terms Black-box testing White-box testing Regression testing
Important terms Black-box testing White-box testing Regression testing
Introduction to Programming in Java
(Computer fundamental Lab)
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
CMPE212 – Reminders Assignment 2 due today, 7pm.
Presentation transcript:

Introduction to Programming with Java Presented by

What is Java? Java is an class object oriented programming language that compiles its programs to byte code and executes within a Java virtual machine. The Java virtual machine enables the program to be (theoretically) architecture agnostic.

Why Use Java? Provides a simpler approach to multi- architecture programming. To some degree provides a portable platform to run executable code. Due to Java’s ability to port the virtual machine to multiple architecture’s, Java is found in devices ranging from toasters, TV’s, mobile phones, cameras, blue-ray players and more.

The Basics of Java Java programs are objects which are instances of a class or classes. Java programs are essentially created by compiling through the Java jdk (Java Development Kit) Java supports the following primitive data-types: o Boolean o Integer o Long o Float o Double o Char o Byte

So technically how simple is it to write a Java program? What do I need? The Java Runtime Environment - JRE The Java Development Kit – JDK Minimally a text editor such as notepad. Those beginning may want an IDE such as o Sun Java development studio o Eclipse o NetBeans

Java Programming Source code for a Java program (or class). Which is the un-compiled program. Is typically stored in files ending with the extension.java. Compiled Java classes (or programs) are stored using the file extension.class. Sometimes the source packages are stored in executable formats with the extension.jar. Typically they are executed through the following command “java –jar ”.

The Basic Process of Designing a Java Program (In Summary) Code Compile (through the JDK) Execute (through the Java VM)

So what does a simple Java program look like? Program CodeOutput – Command Window Import java.lang.*; Public class myProgram { public static void main(String[] args) { System.out.println(“Hello world!”); } “Hello world!”

Defining Variables and Constants Variable(data type) => attribute name ; o IE: int x; float y; char z; Constant => final attribute name = value; o IE: final int thisyr = 2011;

Java Syntax Usage Simple assignments listed above by using the = sign or small expression. o Ie: int e = 1; o IE: String str = “Hello”; o Ie: int j = 1+e; Invoking procedures o Procedure name (Optional parameters) o IE: printMyName(nameIn); printMyName();

Java Syntax – Math Operators + for addition - for subtraction * for multiplication / for division % (modulus) the remainder after division Math.sqrt(x) the square root of a number x. Math.abs(x) the absolute value of a number x. Math.sin(x), Math.cos(x), Math.tan(x)

Java Syntax – Comparison Operators Comparison operators will return true (when a value falls within the comparison parameter) or false when it does not. o < less than o <= less than or equal to o == equal to o >= greater than or equal to o > greater than o != not equal To help compare ranges you can use the following statements with in your statements. o && implies AND o || implies OR

Java Syntax Compound syntax o Selection statements If If/else Switch/case o Repetition / iteration while Do For

Java Syntax - Continued Functions : Are procedures that are called upon can take a parameter, but are required to return a data value. o Attribute functionname (parameter list) o IE: int addOneToX(int x) { x = x+1; return x; } String helloName(String nameIn) { return “Hello “ + nameIn; }

Java Syntax - Arrays Arrays are collections of a single data-type either in a single dimension or a multi-dimension. Array sizes are declared before the array can be used. Things can be placed into a direct index and thus can be retrieved through that index.

Array - Sample int myNumbers[] = new Int[4]; myNumbers[0] = 4; myNumbers[1] = 2; myNumbers[2] = 3; myNumbers[3] = 5; System.out.println(myNumbers[0]); Output

Other Java statements Exceptions – Occur when a input or error in processing occurs in which the program (or in rare circumstances) the java virtual machine is unable to cope with. o In a “perfect world”, make sure that inputs are within in processing range and/or data-type. o Or use a Try/Catch statement.

Over Java statements Casting – The ability to force a data type into another data type. This is different from parsing or converting. o Take and integer num1 and a double num2. Convert num1 to num2. Double num2 = (double) num1; Or -> int num1 = (int)num2; o In a class scenario. Jaguar rotateMtr = (Jaguar)getSpeedController();

Java stores libraries into classes which are stored into packages. Typically called through the import command. o Ie: import java.lang.*;, import java.io, import java.net.*; etc…

What is a class and how is it used? “A class is an unstructured type that encapsulates a fixed number of data components (data members) along with the functions (called member functions or methods) that manipulate them.” - CST 271 Notes.

Class Example Take a house for example. What describes a house? o Size o Number of bedrooms o Number of bathrooms o Address o Color o Does it have a garage…? If so how big is it?

Class – House example Public class house { public String color; public String address; public double totalSize = 0.0; public int numberOfBedrooms = 0; public int numberOfBathrooms = 0; public boolean doesGarageExist = false; public double sizeOfGarage = 0.0; //End variable declaration. public house() { //This is the constructor for the object. } public String returnSumary() { String out = “The house at “ + this.address + “\n”; out += “has “ + this.numberOfBedrooms + “ number of bedrooms\n”; out += “ and “ + this.numberOfBathrooms + “ number of bathrooms\n”; if(this.doesGarageExist) { out += “This house contains a “ + this.sizeOfGarage + “ sq. feet garage\n”; } out += “The house color is “ + this.color + “\n”; out += “The house contains “ + this.totalSize + “ square feet”; return out; }

Implement an instance of the house class CodeOutput Public static class houseProgram { public static void main(String args[]) { house myHouse = new house(); myHouse.color = “blue”; myHouse.totalSize = ; myHouse.numberOfBedrooms = 3; myHouse.doesGarageExist = true; myHouse.sizeOfGarage = 12.0; myHouse.address = “200 blue jay drive”; system.out.print(myHouse.returnSum mary()); } The house at 200 blue jay drive. has 3 bedrooms and 2 bathrooms. The house contains a 12.0 sq. feet garage. The house color is blue. The house contains square feet.

Class Inheritance Class inheritance is the process of extending a parent class. Example creating a mansion class which extends from the house class o Public class mansion extends house In this fashion, methods and certain variables from the parent class are brought into the mansion class. You can build upon those or override them.

Java variable/procedure/function/ class restrictions Can be accessed from the same class Can be accessed from the same package Can be accessed from any child class Can be accessed from any class (default)Yes No privateYesNo protectedYes No publicYes yes

Getting Started with Netbeans

NetBeans – New Generic Project (console application) Click on File -> New Project. Under “Categories” select “Java” Under “Projects” select “Java Application” then click “Next”.

NetBeans – New Generic Project (Console Application) – Continued Under “Project Name” use “MyFirstConsoleApplication”, then click finish.

NetBeans - Layout

NetBeans – Continued

Moving on to FRC Java Robotics – What You Will Need A copy of the CRIO imaging tool (Kit of parts) o Format the CRIO with the Java option. NetBeans with the Java JDK (You may need to have administrative access to install) The FRC WPI java library plug-ins for NetBeans. o Available at First forge. o va_update_for_2011_frc.wpilib_java_frc_2011_update_ve_3

Installing the plug-in to NetBeans Download the FRC WPI library to your system. Extract the ZIP to a (easily found) location. Open Netbeans, under the “Tools” menu select “Plugins”. Under the Plugin dialog choose the “Downloads” tab. Then choose “Add Plugins”. Select all files in the dialog and click “Open” Click “Install”. When prompted. Restart “IDE”.

NetBeans WPI Library Successful Install When the FIRST/WPI Library tools are installed successfully, you should be able to see the following. o FRC Icon in the toolbar. o Template projects o Sample Projects.

FRC Java – Two Types of Robot Projects Simple Robot o By default contains (base methods of robot main) and two methods by default (autonomous and operator control. o Designed for simpler state machines. o Con: Since it is simpler it will continue to run calls made in the autonomous method until everything is done and exits, which could overlap into teleop mode. Iterative Robot o More methods available, which allows for granular control over each mode. o Con: Can cause code to become complex fast. RobotBase o Is what the Simple and Iterative Robot templates are based upon. If you were to make a derivative of this class, you would inherit this class as the parent.

FRC – Simple Robot Go to File -> New Project - > Under the “FRC Java” category, choose “SimpleRobotTemplateProject”. For the project name supply Team _SimpleRobot

FRC – Simple Robot Generated Source Code Expand the “src” folder and “edu.wpi.first.wpi….” folder. Double click on “RobotTemplate.java”

What will we need to get before we can drive? A joystick class – to control the robot. At least two Jaguar or alternative relays. The watch dog class. And Robot drive. To do this, we will call these through the import method. o Import edu.wip.first.wpilibj.*;

Import Statements Import edu.wpi.first.wpilibj.SimpleRobot; Import edu.wpi.first.wpilibj.WatchDog; Import edu.wpi.first.wpilibj.Joystick; Import edu.wpi.first.wpilibj.RobotDrive; Import edu.wpi.first.wpilibj.Timer;

Declare Device Variables Above the void autonomous() add the following. Joystick jsL = new Joystick(1); Joystick jsR = new Joystick(2); RobotDrive rd; Jaguar jag1 = new Jaguar(1); Jaguar jag2 = new Jaguar(2); Jaguar jag3 = new Jaguar(3); Jaguar jag4 = new Jaguar(4);

What it should look like at this point

Write the operatorControl method getWatchDog().setEnabled(true); While(isEnabled() && isOperatorControl()) { getWatchdog().feed(); rd = new RobotDrive(jag1, jag2, jag3, jag4); //Declare inputs to mecanum class. rd.mecanumDrive_Cartesian(jsL.getX(), jsL.getY(), jsL.getTwist(), 0.0); //Add gyro later. }

What it should look like at this point

Now Let’s Run This Before we run this we will have to check a few things. Change the Team number. Go under tools -> options. Choose “Miscellaneous”, then go under “FRC Configuration” tab.

NOTE

Test the Simple Robot When ready click the Green Play icon (aka “Run Main Project”) In the Task pane you will see an output of the cRIO executing the JAVA code. Once the cRIO has restarted, using the FRC driver station you should be able to enable the robot in teleop and drive the robot.

What is going on when I have the robot run the program? Netbeans issues a series of command to compile the java code. Then uses a component called Sunspot to format it to the class code of the Squawk Java ME (VM) The command is issued to restart the cRIO and to look at the robotMain method(). Squawk VM then executes the java program to that line on the cRIO.

Netbeans Running the Java Code

Debugging NetBeans and Squawk (Sunspot) allows you to debug the project while the code is running. Why debug o To understand what is going on with the robot code o To troubleshoot an error(s). How can this be done o Through the output console or breakpoints. Which can be established before you re-deploy the program.

Attach a Debugger Next to the Green Play icon there is a smaller one. Click on the drop down and select “Attach Debugger”. Supply 2900 in the Port field.

NetBeans Layout Will Change (Debug Mode)

Debug – When an Error Occurs When an error occurs that is not caught a message will appear in the output console. (Example below). You can use breakpoints to help troubleshoot before the statements.

Adding a println to the Code At any point in the java program, you can add a System.out.println(“”); which will (in the console) print a statement which is useful for debugging purposes.

For Additional Help Team 2648 has made a quick Java code helper for generating the code. It uses drag and drop blocks to visually generate the code.

Resources cts.wpilib/wiki/LearningJava cts.wpilib/wiki/LearningJava ams/FIRST_Place/Workshops_and_Conferences/201 0_Assets/JavaForFRCKickoff.pdf ams/FIRST_Place/Workshops_and_Conferences/201 0_Assets/JavaForFRCKickoff.pdf cts.wpilib/wiki/StartupArgs cts.wpilib/wiki/StartupArgs

Resources Continued uploads/2009/04/FRC-Java-Introduction.pdf uploads/2009/04/FRC-Java-Introduction.pdf

Thank You