Thursday, March 19, 2009
Reverse Print
Friday, March 6, 2009
DivisionPractice
public class DivisionPractice{ .....the name of the class
public static void main(String[]args){
int in1 = JOptionPaneIntegerParseInt(showInputDialog("Enter a numerator:")); set the numerator that has the data type of int
int in2 = JOptionPaneIntegerParseInt(showInputDialog("Enter a Divisor;")); ..set the denominator that has the data type of in also...
int y=n1/n2; ....compute the qoutient
System.out.println(n1+"/" n2+ \t,"is" +y);...dispaly the output.
if(in1!=int){ ....iuf the value is not int or any other data type it will give the,...
System.out.println("enter only an int value");...error message...
}
if(n2!=int){ ...if the value of in2 is not an int or any other data type...
System.out.println("enter only an int value");....error message...
}
if(n1=="q"&&n1=="Q"){ ....if the input is q or Q....
exit();....exit the program
}
if (n2=="q"&&n2=="Q"){if the input is q or Q....
exit();....exit the program
}
}
}
Sunday, March 1, 2009
ArrayList Example with Iterators
import java.util.List;import java.util.ArrayList;import java.util.Iterator;import java.util.ListIterator;import java.util.Collections;import java.util.Random; public class ArrayListExample { public static void main(String[] args) { // ArrayList Creation List arraylistA = new ArrayList(); List arraylistB = new ArrayList(); // Adding elements to the ArrayList for (int i = 0; i < i1 =" arraylistA.iterator();"> "); while (i1.hasNext()) { System.out.print(i1.next() + " , "); } System.out.println(); System.out.print("ArrayList arraylistA --> "); for (int j = 0; j < i2 =" arraylistB.iterator();"> "); while (i2.hasNext()) { System.out.print(i2.next() + " , "); } System.out.println(); System.out.println(); System.out .println("Using ListIterator to retrieve ArrayList Elements"); System.out.println(); ListIterator li1 = arraylistA.listIterator(); // next(), hasPrevious(), hasNext(), hasNext() nextIndex() can be used with a // ListIterator interface implementation System.out.println("ArrayList arraylistA --> "); while (li1.hasNext()) { System.out.print(li1.next() + " , "); } System.out.println(); // Searching for an element in the ArrayList int index = arraylistB.indexOf("java"); System.out.println("'java' was found at : " + index); int lastIndex = arraylistB.lastIndexOf("java"); System.out.println("'java' was found at : " + lastIndex + " from the last"); System.out.println(); // Getting the subList from the original List List subList = arraylistA.subList(3, arraylistA.size()); System.out.println("New Sub-List(arraylistA) from index 3 to " + arraylistA.size() + ": " + subList); System.out.println(); // Sort an ArrayList System.out.print("Sorted ArrayList arraylistA --> "); Collections.sort(arraylistA); System.out.print(arraylistA); System.out.println(); // Reversing an ArrayList System.out.print("Reversed ArrayList arraylistA --> "); Collections.reverse(arraylistA); System.out.println(arraylistA); System.out.println(); // Checking emptyness of an ArrayList System.out.println("Is arraylistA empty? " + arraylistA.isEmpty()); System.out.println(); // Checking for Equality of ArrayLists ArrayList arraylistC = new ArrayList(arraylistA); System.out.println("arraylistA.equals(arraylistC)? " + arraylistA.equals(arraylistC)); System.out.println(); // Shuffling the elements of an ArrayList in Random Order Collections.shuffle(arraylistA, new Random()); System.out .print("ArrayList arraylistA after shuffling its elements--> "); i1 = arraylistA.iterator(); while (i1.hasNext()) { System.out.print(i1.next() + " , "); } System.out.println(); System.out.println(); // Converting an ArrayList to an Array Object[] array = arraylistA.toArray(); for (int i = 0; i < array.length; i++) { System.out.println("Array Element [" + i + "] = " + array[i]); } System.out.println(); // Clearing ArrayList Elements arraylistA.clear(); System.out.println("arraylistA after clearing : " + arraylistA); System.out.println(); }}
Output
ArrayList arraylistA --> 0 , 1 , 2 , 3 , 4 ,ArrayList arraylistA --> 0 , 1 , 2 , 3 , 4 ,ArrayList arraylistB -->beginner , java , tutorial , . , com , java , site ,
Using ListIterator to retrieve ArrayList Elements
ArrayList arraylistA -->0 , 1 , 2 , 3 , 4 ,'java' was found at : 1'java' was found at : 5 from the last
New Sub-List(arraylistA) from index 3 to 5: [3, 4]
Sorted ArrayList arraylistA --> [0, 1, 2, 3, 4]Reversed ArrayList arraylistA --> [4, 3, 2, 1, 0]
Is arraylistA empty? false
arraylistA.equals(arraylistC)? true
ArrayList arraylistA after shuffling its elements--> 3 , 2 , 1 , 0 , 4 ,
Array Element [0] = 3Array Element [1] = 2Array Element [2] = 1Array Element [3] = 0Array Element [4] = 4
arraylistA after clearing : []
Source: http://www.javabeginner.com/java-arraylist.html
Explanation:
Java ArrayList
public class ArrayList
Resizable-array implementation of the List interface. A java ArrayList is used to store an "ordered" group of elements where duplicates are allowed.
Implements all optional list operations, and permits all elements, including null.
This class is similar to Vector, except that it is unsynchronized.
The size, isEmpty, get, set, iterator, and listIterator operations run in constant time. ArrayList's give great performance on get() and set() methods, but do not perform well on add() and remove() methods when compared to a LinkedList.
An ArrayList capacity is the size of the array used to store the elements in the list. As elements are added to an ArrayList, its capacity grows automatically. It is an Array based implementation where elements of the List can be accessed directly through get() method.
To prevent unsynchronized access to the list: List list = Collections.synchronizedList(new ArrayList(...));
Sunday, February 8, 2009
Class Shirt
public static void main(String[]args){
public int ShirtID;
public int Price;
public String Color;
public String Description;
public int QuantityStock;
public Shirt(int ShirtID, int Price){
ShirtID=SID;
Price=P;
}
public Shirt(String Color, String Description){
Color=C;
Description=D;
}
public Shirt(int QuantityStock){
QuantityStock=QS;
String[]Shirt=new String[sleeveless, denim, long sleeve, turtle neck, body fit,];
My Shirt=new Shirt();
add.Shirt=add Shirt();
System.out.println("Here are our Shirts that are available:"+Shirt[]);
}
}
}
Class FormOfPayment
public static void main(String[]args){
public int CheckNumber;
public int CreditCardNumber;
public int ExpirationDate;
public FormOfPayment(int CheckNumber, int CreditCardNumber, int ExpirationDate){
ChechNumber=CNum;
CreditCardNumber=CcardNum;
ExpirationDate=Expdate;
}
}
}
Class Order
public static void main(String[]args){
public intb Order;
public int TotalPrice;
public String Status;
public Order(int OrderID, int TotalPrice, String Status){
OrderID=OrId;
Price=+price;
Status=Stat.
Order= new Order();
int FormOfPayment1= CheckNumber();
int FormOfPayment2= CreditCardNumber();
}
}
}