Programming 1, Tutorial 1

For our first tutorial, we had to go through and answer some questions about programming and Java, here are my answers:

1. Product Development Life Cycle

Editing

Writing source code

Saving

Recording or storing the file for future access.

Compiling

Translate source code (human readable) into platform specific executable machine code. So for each platform, you need to compile a version for that platform. Not for Java.

Execute

Run the compiled code.

Debugging

Check for coding errors like syntax, logic and runtime errors.

2. When Java Compiles

  1. Create Source Code
  2. Java compiler
  3. Compiled into platform-independent bytecode.
  4. Compiled bytecode can be executed on any platform that has the Java Runtime Environment.

3. Running a Java Program

Compile: javac <filename>.java

Run: java <filename>

To compile, the JDK is required. To run, the JRE or JDK is required.

4. The Main Method

The main statement is preceeded by the following:

public static void

‘public’  allows processes outside of the class access the main statement.

‘static’ allows us to call that method without having any instances of the parent object existing.

‘void’ indicates there’s no return value

 

5. Identifiers

Identifiers are the handle used to access an object, primitive variable, or method.

Identifiers can have:

  • Letters
  • Numbers
  • Underscore and Dollar Sign

They can not:

  • Contain spaces
  • Be a reserved keyword

a) Yes  b) No c) yes d) yes e) no f) yes g) yes h) yes i) no j) no k) no l) no m) yes n) yes

Conventions for Identifiers

Pick a consistent naming system.

Primative variables should begin with  a lowercase, and words can be separated like:

Int numberStudents;

Int number_students;

Constants should be upper case, like:

Final NUMBER_STUDENTS;

6. Example Code

public class HelloWorld

{

public static void main(String[] args)

{

System.out.print(”Welcome “);

System.out.println(”To “);

System.out.print(”RMIT”);

}

}

a)      Class declaration must be specified because a java app needs at least 1 class.

b)      ‘main’ is required because it’s the entry point of our project. Its purpose is to create required objects and call other required methods.

c)      System.out.print(”Welcome)… calls the System.out object, referring to the output stream. It throws “Welcome” to the console.

d)      System.out.print prints at the current cursor position, System.out.print prints and then moves the cursor to the next line.

e)      In unix, it would produce:
Welcome To
In windows, it would produce:
Welcome To
RMIT
Refer to Extra, Unix Buffer for the reason.

7. Debugging Syntax

// /* programs need

comments */

public class OneOne

{

public static void main(String[] args)

{

aString = new String(”Well!”);

int 2ndString = “Now!”;

System.out.println(”Isn’t this an interesting program);

System.out.println(”Answer yes or no!”)

}

}

  • Errors: Comments are incorrectly formatted
  • Note: classes are public by default, but it’s a good idea to include public.
  • aString’s type hasn’t been declared (aString = new String(”Well!”);
  • Tried to string data into an int type var int 2ndString = “Now!”;
  • Invalid variable name 2ndString 2ndString
  • No closing quote System.out.println(”Isn’t this an interesting program);
  • No semicolon System.out.println(”Answer yes or no!”)

Extra:

Unix Buffer:

In Unix, output won’t print until it receives a newline character “\n”

Output Streams:

.out

.err

Input Streams:

.in

Tags: , , , ,

Leave a Reply