Tuesday, October 2, 2007

Continuation of Class 2

ScrollBars

As you know, when you make a JOP.SMD(null,"Message"); a small pop up window will come, sometimes you need MORE than that. You want to have a window with scroll bars to have enough space for all the information you'd like to enter.

Well, this is what you need to do:

JTextArea textBox = new JTextArea(20,20);
textBox.setText(someString);
JOP.SMG(null, new JScrollPane(textBox));

And well, that's it.

-----

Now, we are going to move on, and I'll post two of the assignments you can try, and if you have any questions you can post comments, and such.

In this exercise you are to create a new main class to do the following:
  • Declare a String variable and assign to it a String object constructed from your name.

  • Write a statement to display the variable in a JOptionPane window.

  • Declare an int variable and assign to it the value of the String variable's length method.

  • Write a statement to display the variable in a JOptionPane window.

  • Declare a String variable named out and assign to it the name variable followed by a space followed by the int variable (use the + operator).

  • Construct a BankAccount object using "123456789" as the account number and assign it to a Bank Account variable. The BankAccount class is available from Lab Exercise One.

  • Deposit 100 dollars into the account using the variable and the BankAccount class's deposit method.

  • Use the + operator to concatenate the BankAccount variable to the String variable out.

  • Construct a JTextArea with 20 rows and 20 columns and assign it to a JTextArea variable.

  • Use the setText method of JTextArea to output the String variable out on the JTextArea.

  • Construct a JScrollPane from the JTextArea and display it in a JOptionPane window.

  • Run your program.

Enjoy!

SOLUTION FOR THE ASSIGNMENT -- DO IT, TRY IT, ASK QUESTIONS, IF YOU STILL CAN'T FIGURE IT OUT, LOOK BELOW.


import javax.swing.*;
/**
* Write a description of class Main here.
*
* @author Pablo
* @version (a version number or a date)
*/
public class Main
{
/**
* Constructor for objects of class Main
*/
public Main()
{
// initialise instance variables
}
public static void main(String[] args)
{
String myName = "Your Name";
JOptionPane.showMessageDialog(null,myName);
int nameLength = myName.length();
JOptionPane.showMessageDialog(null,"Name length is: " + nameLength);
BankAccount userAcc = new BankAccount ("123456789");
userAcc.deposit(100.00);
String accountBalance = userAcc.getAccountNumber() + " " + userAcc.getBalance();
JTextArea scrollArea = new JTextArea(20,20);
scrollArea.setText(accountBalance);
JOptionPane.showMessageDialog(null, new JScrollPane(scrollArea));
}
}

Class 2

BankAccount class* (http://rapidshare.com/files/59886458/BankAccount.java.html)
You need NETBEANS to code in Java

An object of the BankAccount class has a String as an account number, and a double indicating your balance.

void deposit (double amount)
void withdraw (double amount)
double getBalance()

A variable in Java is he name of a place to store values.
Variable names start with a lowercase letter (like methods).
Variables can only hold one type of data, so the type must be declared.

synatax: type variableName;

Examples:
- int numberOfItems;
- double price;
- String word;
- BankAccount myAccount;

Assignment operator is =

numberOfItems=37;
price = 15.99;
word = new String("abcde") or word = "abcde";

A constructor of a class object is a method of the class that has the same name as the class. It can have inputs like any other class but its description does not have a return type.

new ClassName(input)

To "call" a method to operate on an object we use the dot (.) notation

syntax: variable.method(...)

int lenght= word.length();
myAccount.deposit(10000.00);

Primitive types (int, char, double, boolean) hold values and class types (BankAccount, String, ...) references to class objects.

Class objects must be constructed using the class method called a constructor. A constructor method has the same name as the class and new returns a reference.

SomeClass someClassVariable = new someClass(...);

To access methods of the object that someClassVariable refers to, use . notation someClassVariable.aMethod(...);


Examples:

BankAccount anAccount = new BankAccount("377");
anAccount.deposit(100.0);
JOptionPane.ShowMessageDialog(null, anAccount.getBalance());
String aWord = new String("abcde");
String anotherWord = "12345";
String s= aWord + anotherWord; This equals: abcde12345
*JOP.SMD(null, "Balance is" + anAccount.Balance());
Abbreviation of JOptionPane.~

+ is the concatenation operator for the String class



Methods of the String class


int length()
char charAt(int position) where 0<=positionint indexOf(char ch) returns left most position of ch; returns -1 if ch is not there
int indexOf(String s) same as above
String substring(int frmPosition) returns the substring starting at frmPosition to the end
where 0<=frmPosition<=length


First Class

Java is an OOP (Object Oriented Programming) language i,e Java has classes.

A class is a description of an object
An object contains data and methods (We can call it instructions)
An object is like a special purpose computer.
A class is like the specs of the object computer.

There are 3 kinds of classes:

(I) Main class: contains the main method i,e, the start instruction
(II) Utility class: general tools
(III) Data class: used and defined by programmer i,e, custom class

A data type is a collection of values and operations on those values. Example: A class

Primitive types in Java:

int:
values: 37, -4, 0
operations: +, -, *, /, %

double:
values: 37.0, 14.99, -3.2
operations: +, -, *, /

char:
values: 'a', '3', '\', '\\'

boolean:
values: true, false
operations: !, &&, ||

To create an object of a class use the class constructor.
Classes have descriptions of methods.
A description has the following form:

return-type name(input)
or
void name(input)

NOTE:

pricePerItem -> method
PricePerItem -> class

String class

An object of the String class is a list (or sequence) of characters. The positions are 0, 1, 2, 3...

Example: "house" where h is at position 0, o at position 1, etc.

Some methods are:

char charAt(int position);
int indexOf(char ch);
int indexOf(String s);
boolean contains(String s);
String substring(int first, int last);
String replace(s1, s2); //replaces instances of s1 by s2

End of class 1, if questions or would like to give feedback, please, comment.