100 likes | 236 Views
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);
E N D
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(); } }
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”
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
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.
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.
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.
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) {
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.
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