Java Programming: Understanding Classes, Methods, and Variables - Prof. Mohamed Aboutabl, Study notes of Computer Science

An introduction to java programming, focusing on the structure of a java program, classes and methods, variable declaration and initialization, and data types. It includes examples of properly formatted and poorly formatted java code, as well as the use of comments and javadoc format.

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-kfr
koofers-user-kfr 🇺🇸

10 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS139 – Algorithm Development 09/06/04
Notes
Important terms from today’s lecture in no particular order:
program
application
comment
whitespace
indenting
block vs inline
comment
class
method
identifier
block
statement
parameter
reserved word
case sensitive
declaration
assignment
int
constant/named
constant
strongly typed
language
data type
primitive data type
literal
String literal
page 1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Java Programming: Understanding Classes, Methods, and Variables - Prof. Mohamed Aboutabl and more Study notes Computer Science in PDF only on Docsity!

CS139 – Algorithm Development 09/06/ Notes Important terms from today’s lecture in no particular order: program application comment whitespace indenting block vs inline comment class method identifier block statement parameter reserved word case sensitive declaration assignment int constant/named constant strongly typed language data type primitive data type literal String literal page 1

© 2004 Pearson Addison-Wesley. All rights reserved 1 - 47

Java Program Structure

public class MyProgram { } // comments about the class public static void main (String[] args) { } // comments about the method method header method body

Basic program – heading – inline comments Class – name begins with capital letter Inline comments Comment for overall program and the method Every statement ends in a semi-colon. Every statement in java has a particular syntax. See Appendix L for complete syntax. //**************************************************************** // Lincoln.java Author: Lewis/Loftus // // Demonstrates the basic structure of a Java application. //**************************************************************** public class Lincoln { //------------------------------------------------------------- // Prints a presidential quote. //------------------------------------------------------------- public static void main (String[] args) { System.out.println ("A quote by Abraham Lincoln:"); System.out.println ("Whatever you are, be a good one."); } }

Third example incorporates our classroom style //**************************************************************** // // Name: Lewis/Loftus // Date: 09/06/ // Assignment: PianoKeys.java // //**************************************************************** // PianoKeys class // Demonstrates the declaration, initialization, and use of an // integer variable. //**************************************************************** public class PianoKeys { /*------------------------------------------------------------ // Prints the number of keys on a piano. //-----------------------------------------------------------/ public static void main (String[] args) { int keys; // keys will hold the number of piano keys keys = 88; // number of keys on a standard piano System.out.println ("A piano has " + keys + " keys."); } } Main method is described using javadoc format. method name, main, is in lower case. keys is a descriptive variable name in lower case. Declaration associates a memory location with a name and a length (type). Declaration and initialization are separated into two statements. Everything within the class block is indented three spaces. Everything within the main block is indented three additional spaces. Header to include the author’s name, date and assignment. A description of the overall class. (If this were a programming test, the header would have been followed by the reference section.) Class name is in Title case

Final example shows more complex program using successive assignment. //**************************************************************** // Author: Lewis/Loftus // Date: 09/06/ // Assignment: Geometry.java //**************************************************************** // Demonstrates the use of an assignment statement to change the // value stored in a variable. //**************************************************************** public class Geometry { /*----------------------------------------------------------- // Prints the number of sides of several geometric shapes. //----------------------------------------------------------/ public static void main (String[] args) { int sides; // stores number of sides for the shape // Begin changing number of sides and printing shape. sides = 7; System.out.println ("A heptagon has " + sides + " sides."); sides = 10; System.out.println ("A decagon has " + sides + " sides."); sides = 12; System.out.println ("A dodecagon has " + sides + " sides."); } } Declaration is separated from rest of the code. Each assignment print group is separated by whitespace. Program and main method are documented. Inline comments describe the variable, sides, and the actions to be carried out.

Data and data types int is a data type (can also be called an abstract data type). Used to hold integer values. It is a primitive type – it is built into the language. Java has 6 different numeric primitive data types. int byte short long float double See chart on page 74 Numeric representation of numbers means that the float numbers are approximation. Double has more precision than float. Java has 2 other primitive data types. char boolean All variables must be declared before use. Java is strongly typed. If you declare a variable of a particular type, you cannot use it to hold a value of a different type.