1 / 10

Simple Java Program

Simple Java Program. Simple Karel J Program. package org.consiliumtemporarium ; import kareltherobot .*; public class RobotRunner implements Directions { public static void main(String[] args ) { World. setVisible ( true ); UrRobot karel = new UrRobot (1, 1, East, 0);

Download Presentation

Simple Java Program

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Simple Java Program

  2. Simple Karel J Program package org.consiliumtemporarium; import kareltherobot.*; public class RobotRunner implements Directions { public static void main(String[] args) { World.setVisible(true); UrRobotkarel = new UrRobot(1, 1, East, 0); karel.move(); karel.turnLeft(); karel.move(); karel.move(); karel.turnOff(); } }

  3. Java Packages package org.consiliumtemporarium; • Group related classes together • All of the classes for a package can be put into a Java ARchive (JAR file) • Typically based on a organization/company name followed by the type of classes • Sometimes called a “namespace”

  4. Importing packages import org.consiliumtemporarium.Shape;import org.consiliumtemporarium.*; • Import classes from another package. Many classes are already defined, this lets us use other people work • org.consiliumtemporarium.* is shorthand for all classes from the “org.consiliumtemporarium” package

  5. Class Definition public class RobotRunner implements Directions { • Define the class for the file. Directions is an interface that the RobotRunner class implements. Interfaces will be discussed later. • Class names in java should capitalize each word.

  6. Main method public static void main(String[] args) { • Java uses this method to know where to start the java application. • Classes don’t have to have a main method, but at least one class in an application must have the main method. • Method names in java don’t capitalize the first word, but every word after that.

  7. Karel Robot World Classes World.setVisible(true); • Creators of Karel J Robot created the World class to show the robot world. • If we don’t make the robot world visible our program will run, but not show us anything! • Later we’ll see another World method:World.readWorld(“robotWorldFile.kwld”);This loads up a robot world that someone created.

  8. Comments • Programs should have good comments • Method and class definitions should follow the standard java commenting format:/** * This is my class */public class MyClass { /** * Main method */ public static void main(String[] args) {

  9. Comments • Comments inside a method can have two formats:public void myMethod() { /* This comment style can go over multiple lines */ // This style is for a single line • You should have comments inside a method explaining what’s going on. But don’t be overly verbose.

  10. Style • The style of java source code is something people debate a lot. What kind of style you use doesn’t matter so much as being consistent. • Indenting, parenthesis, braces, and where there are blank lines should be consistent in your code

More Related