



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
1 / 6
This page cannot be seen from the preview
Don't miss anything!
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
There’s a built in LL too It can only hold objects (not primitives) LinkedList