Friday, February 11, 2011
Assignment 5 - GradeBookTest
// GradeBookTest.Java
// Creating and manipulating a GradeBook object.
import java.util.Scanner; // Program uses Scanner
public class GradeBookTest
{
//main method begins program execution
public static void main( String[] args )
{
// create Scanner to obtain input from command window
Scanner input = new Scanner( System.in );
// create a GradeBook object and assign it to myGradeBook
GradeBook myGradeBook = new GradeBook();
// display initial value of courseName
System.out.printf( "Initial course name is; %s\n\n",
myGradeBook.getCourseName() );
// prompt for and read course name
System.out.println( "Please enter the course name:" );
String theName = input.nextLine(); // read a line of text
myGradeBook.setCourseName( theName ); // set the course name
System.out.println(); // outputs a blank line
// display welcome message after specifying course name
myGradeBook.displayMessage();
} // end main
} // end class GradeBookTest
Wednesday, February 2, 2011
Assignment 4 - SumArray
// Fig. 6.5: SumArray.java
// Computing the sum of the elements of an array.
public class SumArray
{
public static void main(String[]args)
{
int[]array={87, 68, 94, 100, 83, 78, 85, 91, 76, 87 };
int total=0;
// add each element's value to total
for(int counter=0; counter<array.length;counter++)
total +=array[counter];
System.out.printf("Total of array elements;%d\n",total);
} // end main
} // end class SumArray
Assignment 3 - InitArray
// InitArray.java
// Calculation values to be placed into elements of an array.
public class InitArray
{
public static void main(String[]args)
{
final int ARRAY_LENGTH=10; // constant
int[] array=new int[ ARRAY_LENGTH]; // create array
// calculate value for each array element
for(int counter=0; counter<array.length;counter++)
array[counter]=2+2*counter;
System.out.printf("%s%8s\n","Index","Value"); // column heading
// out put each array element's value
for(int counter=0; counter<array.length;counter++)
System.out.printf("%5d%8d\n",counter,array[counter]);
} // end main
} // end class InitArrway
Assignment 2 - DrawPanel
// Assignment 2
// DrawPanel.java
// Using drawLine to connect the corners of a panel.
import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.JFrame;
public class DrawPanel extends JPanel
{
// draws an X from the corners of the panel
public void paintComponent( Graphics g)
{
// call paintComponent to ensure the panel displays correctly
super.paintComponent(g);
int width = getWidth(); //total width
int height=getHeight(); // total height
// draw a line from the upper.left to the upper-right
g.drawLine(0, height, width, 0);
} // end method paintComponent
public static void main(String[]args)
{
// create a panel that contains our drawing
DrawPanel panel=new DrawPanel();
// create a new frame to hold the panel
JFrame application=new JFrame();
// set the frame to exit when it is closed
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add(panel); // add the panel to the frame
application.setSize( 250, 250 ); // set the size of the frame
application.setVisible( true ); // make the frame visible
} // end main
} // end class DrawPanel
Assignment 1 - Increment
// Assignment 1
// Fig. 3.13: Increment.java
// Prefix increment and postfix increment operators.
public class Increment
{
public static void main(String[]args)
{
int c;
// demonstrate postfix increment operator
c=5; // assign 5 to c
System.out.println(c); // prints 5
System.out.println(c++); // prints 5 then postincrements
System.out.println(c); // prints 6
System.out.println(); // skip a line
// demostrate prefix increment operator
c=5; // assign 5 to c
System.out.print(c); // prints 5
System.out.println(++c); // preincrements then prints 6
System.out.println(c); // prints 6
} // end main
} // end class Increment
// Fig. 3.13: Increment.java
// Prefix increment and postfix increment operators.
public class Increment
{
public static void main(String[]args)
{
int c;
// demonstrate postfix increment operator
c=5; // assign 5 to c
System.out.println(c); // prints 5
System.out.println(c++); // prints 5 then postincrements
System.out.println(c); // prints 6
System.out.println(); // skip a line
// demostrate prefix increment operator
c=5; // assign 5 to c
System.out.print(c); // prints 5
System.out.println(++c); // preincrements then prints 6
System.out.println(c); // prints 6
} // end main
} // end class Increment
Subscribe to:
Posts (Atom)