Classes are type specifiers that specify the types of objects. (int, char, Boolean, float, etc., are among other type specifiers)
HL ; computer programming ; all languages
Monday, November 22, 2010
Java – fields of an object (instance variables, as opposed to local variables ) have default initial values in Java
Note that if you directly print out the value of a declared but uninitialized field that was declared as of type String, you would get “null” displayed on the screen instead of junk. Unlike local variables, which are not automatically initialized, every field has a default initial value – a value specified by Java when you do not initialize the field value.
**The default value for a field of type String is null.
Java - Where to declare your instance variables
In theory, in Java you can list your class’s fields anywhere in the class outside of the class’s method declarations, but scattering tends to result in code that is hard to read, therefore, we like to list a class’s fields first.
Java - General Rules of Thumb
In general, instance variables should usually be declared private, and methods should usually be declared public (there are times when it is also appropriate to declare methods private, when you only what other methods in the same class to be able to access the method)
Java - Data Hiding / Encapsulation
The practice of declaring instance variables using the private access modifier is called data hiding (encapsulation). => can only be accessed by methods of the same object’s class. => this prevents the object’s instance variable from being accidentally changed by some other class in the program.
Java - Data Hiding / Encapsulation
The practice of declaring instance variables using the private access modifier is called data hiding (encapsulation). => can only be accessed by methods of the same object’s class. => this prevents the object’s instance variable from being accidentally changed by some other class in the program.
Java - Access Modifiers public and private
Most instance variables are declared to be private. This makes them accessible only to the methods in the class from which they are declared.
Java – Instance variable
All methods of the class can manipulate any instance variables that appear in the class.
Java - What is the difference between String and String[] ?
String is just one string.
String is an array of String's.
String is an array of String's.
Sunday, November 21, 2010
Java - Example - mechanism to note: method in a Class calling another method in the same Class
mechanism to note: method in a Class calling another method in the same Class
Java - Example - A Class with an Instance Variable, a set Method and a get Method
In this example, our class maintains the course name as an instance variable, so that it can be used or modified at any time during our application's execution.
Our class contains 3 methods - setCourseName, getCourseName, and displayMessage.
The method setCourseName stores a course name in a GradeBook (on object instance of our class)
The method getCourseName obtains a GradeBook's course name.
The method displayMessage, which now has no parameters being passed into it, still displays a welcome message, but also includes the course name of that object in the welcome message => what you should note is, displayMessage now obtains the course name be calling another method in the same class [ the getCourseName method] !!
Reference: "Java How to Program, by Deitel & Deitel"
Our class contains 3 methods - setCourseName, getCourseName, and displayMessage.
The method setCourseName stores a course name in a GradeBook (on object instance of our class)
The method getCourseName obtains a GradeBook's course name.
The method displayMessage, which now has no parameters being passed into it, still displays a welcome message, but also includes the course name of that object in the welcome message => what you should note is, displayMessage now obtains the course name be calling another method in the same class [ the getCourseName method] !!
Reference: "Java How to Program, by Deitel & Deitel"
Saturday, November 20, 2010
Java - Instance Variables (2)
Classes normally have methods that manipulate the attributes that belong to object instantiations of the class.
Attributes are realized in the form of variables inside a class declaration. These attributes(variables) are declared inside the class declaration but outside the bodies of the class's methods.
**When each object of a class maintains its own copy of an attribute, the field that represents the attribute is also known as an "instance variable" - each object (instance) of the class has a separate instance of the variable in memory.
Reference: "Java How to Program, by Deitel & Deitel"
Attributes are realized in the form of variables inside a class declaration. These attributes(variables) are declared inside the class declaration but outside the bodies of the class's methods.
**When each object of a class maintains its own copy of an attribute, the field that represents the attribute is also known as an "instance variable" - each object (instance) of the class has a separate instance of the variable in memory.
Reference: "Java How to Program, by Deitel & Deitel"
Java - Instance Variables
Prior to this point, all the the variables we used we declared in an application's main method.
When a variable is declared in the body of a method, it is categorized as a "local variable', and that variable can only be used inside that method.
When the particular method terminates (right after it finishes execution), all values of its local variables terminate with it.
**On the other hand, objects can have attributes that it carries with it, regardless of whether one of its methods are in execution or have finished execution or not.
**These attributes exist before a method is called (as soon as an instance of the object is instantiated), and continues to exist independent of the execution or completion of any of its methods.
When a variable is declared in the body of a method, it is categorized as a "local variable', and that variable can only be used inside that method.
When the particular method terminates (right after it finishes execution), all values of its local variables terminate with it.
**On the other hand, objects can have attributes that it carries with it, regardless of whether one of its methods are in execution or have finished execution or not.
**These attributes exist before a method is called (as soon as an instance of the object is instantiated), and continues to exist independent of the execution or completion of any of its methods.
Friday, November 19, 2010
Java - Creating a GradeBook Object and passing a String to its displayMessage method
import java.util.Scanner;
public class GradeBookTest
{
public static void main(String[] args)
{
Scanner myScanner = new Scanner(System.in);
GradeBook myGradeBook = new GradeBook();
System.out.println("HL: Please enter your course name: ");
String nameOfCourse = myScanner.nextLine();
myGradeBook.displayMessage(nameOfCourse);
} //end main
} //end GradeBookTest class
Reference: "Java How To Program, by Deitel & Deitel"
public class GradeBookTest
{
public static void main(String[] args)
{
Scanner myScanner = new Scanner(System.in);
GradeBook myGradeBook = new GradeBook();
System.out.println("HL: Please enter your course name: ");
String nameOfCourse = myScanner.nextLine();
myGradeBook.displayMessage(nameOfCourse);
} //end main
} //end GradeBookTest class
Reference: "Java How To Program, by Deitel & Deitel"
Java - Class Declaration with a Method that has a Parameter
public class GradeBook
{
public void displayMessage(String courseName)
{
System.out.printf("Howard welcomes you to the gradebook for %s \n", courseName);
} //end displayMessage method
} //end GradeBook class
Reference: "Java How to Program"
{
public void displayMessage(String courseName)
{
System.out.printf("Howard welcomes you to the gradebook for %s \n", courseName);
} //end displayMessage method
} //end GradeBook class
Reference: "Java How to Program"
Sunday, October 31, 2010
Java - Intro to Classes and Objects
If you become part of a development team in industry, you might work on applications that contain hundreds, or even thousands, of classes.
excerpt from "Java how to program, by Deitel" page 73
excerpt from "Java how to program, by Deitel" page 73
JAVA - the System class. And, the package java.lang.
System.out.print("Enter first integer: "); //prompt
System is a class. Class System is part of package java.lang. Notice that class System is not imported with an import declaration at the beginning of the program.
**By default, package java.lang is imported in every Java program; thus, classes in java.lang are the only ones in the java API that do not require an import declaration.
excerpted from "Java how to program,by Deitel"
System is a class. Class System is part of package java.lang. Notice that class System is not imported with an import declaration at the beginning of the program.
**By default, package java.lang is imported in every Java program; thus, classes in java.lang are the only ones in the java API that do not require an import declaration.
excerpted from "Java how to program,by Deitel"
JAVA - Libraries and APIs documentation
Excerpt from "Java How to Program, by Deitel":
For each new Java API class we use, we indicate the package in which it is located. This information helps you locate descriptions of each package and class in the Java API documentation.
A web version of this documentation can be found at :
java.sun.com/javase/6/docs/api/
You can download this documentation from
java.sun.com/javase/downloads
Appendix E shows how to use this documentation.
For each new Java API class we use, we indicate the package in which it is located. This information helps you locate descriptions of each package and class in the Java API documentation.
A web version of this documentation can be found at :
java.sun.com/javase/6/docs/api/
You can download this documentation from
java.sun.com/javase/downloads
Appendix E shows how to use this documentation.
Tuesday, October 19, 2010
C - One way to get input from keyboard
references:
1. Oualline, S. (1997). Practical C Programming, 3rd ed., O'Reilly Media, Inc.
2. BJ Furman | ME 30 Computer Applications | A Better Way to Get Input From the Keyboard.doc
/* Example of a robust way to get input from the keyboard*/
#include <stdio.h>
/*declare variables that you want to read in*/
char char1;
int var1;
float var2;
char string1[20]; /*an array to hold 19 characters*/
char line[100]; /*for the line of user input*/
/*put a user prompt here*/
/*get and process the user input*/
fgets(line, sizeof(line). stdin);
sscanf( line, "%s %c %f %d", string1, &char1, &var2, &var1);
Step-by-Step:
1. Declare variables you want to use to store the input
2. Declare an array of characters that is long enough to contain what the user will enter
char line[100];
3. Get the line of input and process it to store the variables
fgets (line, sizeof(line), stdin);
sscanf (line, "%s %c %f %d", string1, &char1, &var2, &var1);
How Things Work:
1. fgets() gets a line of input (note: 100 characters maximum including the EOL) from stdin (the keyboard)
2. sscanf() (string scanf()) scans the input line string and processes it according to the control string (i.e., the conversion specifications between the double-quotes)
3. sscanf() must have pointers to the variables (hence ampersands in front of the variable names, and the use of a character array (string[20]) in the parameter list )
4. * Of course the conversion specifications in the control string must match the list of variables in order for this to work properly.
1. Oualline, S. (1997). Practical C Programming, 3rd ed., O'Reilly Media, Inc.
2. BJ Furman | ME 30 Computer Applications | A Better Way to Get Input From the Keyboard.doc
/* Example of a robust way to get input from the keyboard*/
#include <stdio.h>
/*declare variables that you want to read in*/
char char1;
int var1;
float var2;
char string1[20]; /*an array to hold 19 characters*/
char line[100]; /*for the line of user input*/
/*put a user prompt here*/
/*get and process the user input*/
fgets(line, sizeof(line). stdin);
sscanf( line, "%s %c %f %d", string1, &char1, &var2, &var1);
Step-by-Step:
1. Declare variables you want to use to store the input
2. Declare an array of characters that is long enough to contain what the user will enter
char line[100];
3. Get the line of input and process it to store the variables
fgets (line, sizeof(line), stdin);
sscanf (line, "%s %c %f %d", string1, &char1, &var2, &var1);
How Things Work:
1. fgets() gets a line of input (note: 100 characters maximum including the EOL) from stdin (the keyboard)
2. sscanf() (string scanf()) scans the input line string and processes it according to the control string (i.e., the conversion specifications between the double-quotes)
3. sscanf() must have pointers to the variables (hence ampersands in front of the variable names, and the use of a character array (string[20]) in the parameter list )
4. * Of course the conversion specifications in the control string must match the list of variables in order for this to work properly.
Subscribe to:
Posts (Atom)