Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java Programming: Classes, Execution, and Documentation - Prof. Ernest Ackermann, Study notes of Computer Science

An introduction to java programming, focusing on writing classes, executing java programs, and using javadoc for documentation. It covers topics such as constructors, methods, data members, static methods, and exception handling.

Typology: Study notes

Pre 2010

Uploaded on 08/13/2009

koofers-user-z54-1
koofers-user-z54-1 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Using Java
Everything in Java has to be written in a class. You can’t write a non-OO program.
You don’t separate the specification & implementation for a class in Java. It all goes in one
file.
The name of the file has to be the same as the name as the class (______________________
___________________).
public class Greeter
{
//constructor
public Greater (String aName) {
name = aName;
}
//method
public String sayHello( ) {
Return “Hello, ” + name + “!”;
}
//data member
private String name;
}
Can you execute the class above alone? ______________________________
Where does execution of a Java program start?
Execution of a Java program starts with the class that contains the __________________
public class GreeterTester
{
public static void main (String[ ] args) {
Greeter worldGreeter = new Greeter (“World”);
String greeting = worldGreeter.sayHello( );
System.out.println(greeting);
}
}
The signature for the main method always looks like the one above.
What does static mean?
What does String [] args mean?
pf3
pf4
pf5

Partial preview of the text

Download Java Programming: Classes, Execution, and Documentation - Prof. Ernest Ackermann and more Study notes Computer Science in PDF only on Docsity!

Using Java Everything in Java has to be written in a class. You can’t write a non-OO program.  You don’t separate the specification & implementation for a class in Java. It all goes in one file.  The name of the file has to be the same as the name as the class (______________________ ___________________). public class Greeter { //constructor public Greater (String aName) { name = aName; } //method public String sayHello( ) { Return “Hello, ” + name + “!”; } //data member private String name; } Can you execute the class above alone? ______________________________ Where does execution of a Java program start? Execution of a Java program starts with the class that contains the __________________ public class GreeterTester { public static void main (String[ ] args) { Greeter worldGreeter = new Greeter (“World”); String greeting = worldGreeter.sayHello( ); System.out.println(greeting); } } The signature for the main method always looks like the one above. What does static mean? What does String [] args mean?

How do you run a Java program? On paprika, type javac yourFileName (something.java) This runs the compiler & produces a file called yourFileName.class. Then type java yourClassName (no .java) This runs the interpreter which executes your program. You can also start NetBeans on paprika and develop the software using that IDE. You can also use the PC lab & use the netBeans software. Program Documentation Java introduced a tool to produce HTML pages of documentation for your programs. This is super convenient because it lets you share the gist of your program in a standard, easy to read way -and it doesn’t take a lot of extra effort. All you need to do is use “javadoc” style comments in your program. Comments are delimited by /** …. / The first sentence of each comment will be copied into your javadoc. You need to start the sentence with a capital letter and end it with a period. You should also document information about parameters and return types for your methods. To do this, use the @param and @return tags /* Construct a Greeter object that can greet a person or entity. @param aName the name of the greeted person or entity / /* Greet with a “Hello” message. @return a message containing “Hello” and the name of the greeted person or entity */ Java standards say that every class, every method, every parameter, and every return value should have a comment. To generate the javadoc, type *javadoc .java It will produce a .html file for each of your .java files, an index.html file, and a few summary files. You can open the .html file with any standard browser. Javadoc was used to document all of the standard Java packages in the Java API (Application Programming Interface). The whole API javadoc is available on the web at http://java.sun.com/j2se/1.5.0/docs/api/ (just google Java API). Programming basics: Primitive types Start with a lower case letter. Don’t have to be instantiated. These are NOT classes. int, double, char, boolean, float, .. Flow Control if…else, while, for, do…while These all work just like C++ 

Strings can’t be changed once created (contents are constant). Common methods for processing strings: charAt( ) length( )

Pretty much every class has a toString( ) method that allows you to convert a non-string value to a string. Don’t use the == to check for string equality. Why? Instead use .equals( ). Most classes have a .equals method defined. Input Input was once a royal pain in the butt in Java. Now there’s a Scanner class.  Scanner in = new Scanner(System.in); System.out.print(“How old are you?”); Int age = in.nextInt( ); next( ) gets the next white space delimited token nextLine( ) reads the next input line Scanner in – new Scanner(new fileReader(“input.txt”); while (in.hasNextLine()) { String line = in.nextLine(); } Array Lists & Linked Lists ArrayList – can only hold objects not primatives ArrayList countries = new ArrayList( ); countries.add(“Belgium”); countries.add(“Italy”); countries.add(“Thailand”); for (int i= 0; i < countries.size( ); i++) { String country = contries.get(i); System.out.println(country); } You can add/remove in the middle of the array: countries.add(1,”Germany”); countries.remove(0); Items are moved up/down as needed to accommodate the new add

There’s a built in LL too It can only hold objects (not primitives) LinkedList countries = new LinkedList(); countries.add(“Belgium”); countries.add(“Italy”); countries.add(“Thailand”); There’s a built-in iterator too ListIterator iterator = countries.listIterator(); while (iterator.hasNext()) { String country = iterator.next(); System.out.println(country); } To add to the middle, use the iterator iterator = countries.listIterator(); iterator.next(); iterator.add(“France”); To remove from the middle (example removes the second item) iterator = countries.listIterator(); iterator.next(); iterator.next(); iterator.remove(); Arrays Used with primitive types only Must be instantiated int[] number = new int[10]; can’t change length of the array after it’s declared. index it just like a C++ array Static Fields & Methods Static is the reserved word to use when you want to create a class variable (you get one memory location that’s shared among all instances of the class). Final is the reserved word to define constants. A static method is one that doesn’t require instantiation of the class in order to be invoked. (Ex: main method any class)