Thursday, October 9, 2008

Exercise Problems in Java Programming

Array Problems

for DIT111

Write a program that will accept 20 integer numbers in an array. Determine and display all the even numbers. Display also the first and the last element of the array. Class name is ArrayDIT111xxx.

for DIT112

Write a program that can accept five integers in an array. Display the integers from first to last, and then display the integers from last to first. Class name is ArrayDIT112xxx.

for DIT113

Create a program that will accept 20 prices of an item and store it in an array. Compute and display the sum of all prices. Display all the values less than 5.00. Calculate and display the average of the prices, and display all values higher than the calculated average value. Class name is ArrayDIT113xxx.

for BSCS112

Create an array of five strings. Write a program that counts and display the number of vowel in the string that you entered, without regard the case. Class name is ArrayBSCS112xxx.

for BSIT112

Write a program that will accept 10 integer numbers store in an array. Compute and display the average. If the average is below 75 display the average, if the average is above 75 compute and display the square root of the average otherwise display the sum. Class name is ArrayBSIT112xxx.


Java Applet Problems

for DIT211

Create an applet with a Button and TextField. Input your name and display it in an 8pt font when button is cliked. Everytime the user clicks the Button, increase the font size for the displayed name by four points. Remove the Button when the font size exceeds 24 points. Class name is AppletDIT211xxx.

for DIT221

Create an applet with a Button labeled "Who's the greatest?" When the user clicks the button, display your name in a large font. Class name is AppletDIT221xxx.

for BSCS211

Create an applet that asks a user to enter a password into a TextField and then press the enter key. Compare the password to "Rosebud"; if they match, display "Access Granted"; if not, display "Access Denied". Class name is AppletBSCS221xxx.

for BSCOE411

Create a payroll applet named AppletBSCOE411xxx that allows the user to enter two double values - hours worked and hourly rate. When the user clicks a Button, net pay is calculated. Federal withholding tax is subtracted from gross pay (hours worked * hourly rate) based on the following table:

Income ($) ___________________ Withholding (%)
0 to 99.99 ______________________10
100.00 to 299.99 _________________15
300.00 to 599.99 _________________21
600.00 and up ___________________28

Saturday, October 4, 2008

Java Applets

In general, an applet is a small program or utility with limited features, requiring minimal resources, and usually designed to run within a larger program.

A Java applet is such a program written in the Java programming language and typically designed to run from a web browser.

Creating a Java Applet

1 . Write the applet in Java, and save it with a .java file extension, just as when you write a Java application.

2. Compile the applet into bytecode using the javac command, just as when you write a Java application.

3. Write an HTML document that includes a statement to call your compiled Java class.

4. Load the HTML document into a Web browser (such as Mozilla Firefox or Microsoft Internet Explorer), or open the HTML document using the Applet Viewer.

Creating HTML document

1. The tag that begins every HTML document is , which is surrounded by angle brackets.

2. The html within the tag is an HTML keyword that specifies that an HTML document follows the keyword.

3. The tag that ends every HTML document is .

Generally, HTML documents contain other tags with the texts. Of importance at this point is the - tag pair used to run an applet from within an HTML document.

Three attributes (or arguments) are placed within the tag: code, width, and height.

Three attributes of the given object tag:

1. code = followed by the name of the compiled applet you are calling

2. width = followed by the width of the applet on the screen

3. height = followed by the height of the applet on the screen

Running an Applet

Two ways of running an applet:

1. by opening the associated HTML file on a Web browser

2. the appletviewer

Writing a Simple Applet

In creating an applet, you must also do the following:

1. Include import statements to ensure that necessary classes are available.

2. Learn to use some user interface (UI) components, such as buttons and text fields, and applet methods.

3. Learn to use the keyword extends.

Creators of Java created an applet class named JApplet that you can import using the statement,

import javax.swing.JApplet;


JApplet Class


JApplet is the Swing equivalent of the AWT Applet class.It is a simple extension of java.applet.Applet for use when creating Swing programs designed to be used in a Web browser.

As a direct subclass of Applet, JApplet is used in much the same way as the Applet.

A JApplet is a Component, and it is also a Container.

When using the JApplet class, you need the following import statements:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

The extends keyword indicates that your applet builds on, or inherits, the traits of the JApplet class.

Example 1:

//java file... save as .java file

import javax.swing.*;
import java.awt.*;

public class sampleApplet extends JApplet
{
public void init()
{
Container con = getContentPane();
cont.setLayout(new FlowLayout());
JLabel label = new JLabel("Hello world!");
con.add(label);
}
}

------------------------------
// html file... save as .html file

sample java applet html code

Output:
sample java applet output Example 2:

//java file... save as .java file

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class sampleAppletRRA extends JApplet implements ActionListener
{
Container con = getContentPane();
JLabel question = new JLabel("What’s your name?");
JTextField answer = new JTextField(10);
JButton press = new JButton("Press me");

public void init()
{
con.add(question);
con.add(answer);
con.add(press);
con.setLayout(new FlowLayout());
press.addActionListener(this);
}

public void actionPerformed(ActionEvent e)
{
String name = answer.getText();
JOptionPane.showMessageDialog(null, "You pressed the button, " + name);
}
}

------------------------------
//html file... save as .html file

java applet html code

Output:

java applet output with input java applet output


Try to create an applet that will accept 2 integer numbers and display the sum.

Thursday, October 2, 2008

Arrays in Java Programming

Array is the most important thing in any programming language. By definition, array is the static memory allocation. It allocates the memory for the same data type in sequence. It contains multiple values of same types. It also store the values in memory at the fixed size. Multiple types of arrays are used in any programming language such as: one - dimensional, two - dimensional or can say multi - dimensional.

An array is a sequence of memory locations for storing data set out in such a way that any one of the individual locations can be accessed by quoting its index number.


Each item in an array is called an element, and each element is accessed by its numerical index. As shown in the above illustration, numbering begins with 0. The 9th element, for example, would therefore be accessed at index 8.

Declaring Java Arrays

To declare an array, write the data type, followed by a set of square brackets [ ], followed by the identifier name.

ElementType [ ] arrayName;

Example:

int [ ] age;
float grades [ ];
String name [ ];

Initializing Array Variables

To initialize an array, you use the new operator to allocate memory for objects. The new operator is followed by the data type with the number of elements to allocate specified the number of elements to be allocated is placed within the [ ] operator.

Example:

age = new int [5];
grades = new float [10];
name = new String [100];

Like other variables, an array variable can be initialized when it is declared.

ElementType[ ] arrayName = new ElementType [ sizeOfArray];

Example:

int [ ] age = new int [ 5 ];
float [ ] grades = new float [10];
String name [ ] = new String [100];

Sometimes user declares an array and it's size simultaneously. You may or may not be define the size in the declaration time. An array can be also created by directly initializing it with data.

Example:

int [ ] arr = {1, 2, 3, 4, 5};

This statement declares and creates an array of integers with five elements, and initializes this with the values 1, 2, 3, 4, and 5.

String [ ] days = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};

This statement declares and creates an array of string with identifier days and initialized. This array contains 7 elements.

Accessing an Array Element

To access an array element, or a part of the array, use a number called an index or a subscript.

An index number or subscript assigned to each member of the array, to allow the program to access an individual member of the array.

It is an integer beginning at zero and progresses sequentially by whole numbers to the end of the array. Index is from 0 to (sizeOfArray - 1).

Example:

//assigns 5 to the first element in the array
age [0] = 5;

// prints the last element in the array
System.out.println(age[4]);

Here is the code of the program:

import java.io.*;

public class sampleArray
{
public static void main (String args []) throws IOException
{
BufferedReader x=new BufferedReader (new InputStreamReader (System.in));
int a;
int num [] = new int[10];

for ( a=0; a<10;>> ");
num[a] = Integer.parseInt(x.readLine());
}

System.out.print("\n\n The inputted numbers are : \n");

for(a=0; a<10; style="font-weight: bold;">Sample Output:

Sample Array Output
Try to create a program that will display the sum of 5 numbers using one dimensional array.

Wednesday, September 24, 2008

Y4IT participants of STI College Lucena

Attendees of Y4IT 2008




The students of STI College Lucena attended the Y4IT 2008 at UP Diliman last Sept 2 and 3, 2008. The students are from different courses and year levels, the 4th year of CS and IT students, 2nd year of DCET and 4th year of Computer Engineering.






Everyones waiting for dinner "the eat all u can" buffet.


The hotel where the students check-in.


The BSCOE participants of STI College Lucena... Ms. Rhoda Rivares is their adviser.


BSCS batch 2009 attendees of STI College Lucena.. say cheez


The BSIT batch 2009 participants of STI College Lucena...
Prof. Giovanni Juntereal, the adviser of CS and IT students.


DCET participants of STI College Lucena. Adviser is Mr. Dennis Pua