Archive for the ‘Uncategorized’ Category

CreateName

public class CreateName {

public static void main(String[] args) {

if (args.length == 0) {
System.out.println(“No Command Line arguments”);
}
else {
System.out.println(“You provided ” + args.length+ ” arguments”);
for (int i = 0; i < args.length; i++) {
System.out.println("args[" + i + "]: "+ args[i]);
}
System.out.print("The new word generated is: ");
System.out.print(generateNewName(args));
System.out.println("\n");
}
}

public static String generateNewName(String[] str){
String letter="";

for (int i = 0; i < str.length; i++) {
letter=letter + str[i].charAt(1);
}
return letter;
}
}

Getting Input, Conditional Constructs, Array, Command Line

Using Command-line, write a program the counts the number of vowels supplied in the command line as parameters. It must determine the count of each vowel separately. The character count is not case sensitive.
Use String class, charAt() method.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class CommandLineArg {
public static void main(String args[]) throws IOException{
BufferedReader dataIn= new BufferedReader(new InputStreamReader(System.in));
System.out.print(“Enter a word: “);
String word = dataIn.readLine();
countVowel(word);
}

public static void countVowel(String text){
int count = 0;
for (int i = 0; i < text.length(); i++) {
char vowel = text.charAt(i);
if (vowel=='a' || vowel=='e' || vowel=='i'
|| vowel=='o' || vowel=='u') {
count++;
}
}
System.out.println("'"+ text + "'"+" has " + count + " vowels");
}
}

Getting Input, Conditional Constructs, Array, Command Line

Using BufferedReader and JoptionPane, ask for 10 numbers from the user. Use an array to store the values of these 10 numbers. Output on the screen the number with the greatest value.

//BufferedReader
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Exercise5A {

public static void main(String[] args) {
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
int[] num = new int[10];
int counter;
int max = 0;
int totalnumber = 10;

for(counter = 0; counter max)){
max = num[counter];
}
}

System.out.println(“\nThe number with the greatest value is ” + max);
}

}

//JoptionPane
import javax.swing.JOptionPane;

public class Exercise5B {

public static void main(String[] args) {
int[] num = new int[10];
int counter;
int max = 0;
int totalnumber = 10;

for(counter = 0; counter max)){
max = num[counter];
}
}

JOptionPane.showMessageDialog(null,”The number with the greatest value is ” + max);
}

}

Getting Input, Conditional Constructs, Array, Command Line

Get three exam grades from the user and compute the average of the grades. Output the average of the three exams. Together with the average, also include a smiley face : ) in the output if the average is greater than or equal to 60, otherwise : ( .
Use BufferedReader and JoptionPane to get input from the user and output the result.

//BufferedReader

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Exercise4A {

public static void main(String[] args){
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
float grade1=0;
float grade2=0;
float grade3=0;

try{
System.out.print(“Enter grade in 1st exam: “);
grade1= Float.parseFloat(dataIn.readLine());

System.out.print(“Enter grade in 2nd exam: “);
grade2= Float.parseFloat(dataIn.readLine());

System.out.print(“Enter grade in 3rd exam: “);
grade3= Float.parseFloat(dataIn.readLine());
}
catch(IOException e){
System.out.println(“Error.”);
}

float ave= (grade1+grade2+grade3)/3;
float round = Round(ave,2);
if(ave>=60){
System.out.println(“\nAverage: “+round+” :)”);
}
else{
System.out.println(“\nAverage: “+round+” :(“);
}
}

public static float Round(float Rval, int Rpl) {
float p = (float)Math.pow(10,Rpl);
Rval = Rval * p;
float tmp = Math.round(Rval);
return (float)tmp/p;
}
}

//JOptionPane

import javax.swing.JOptionPane;

public class Exercise4B {

public static void main(String[] args){

float grade1=0;
float grade2=0;
float grade3=0;

grade1 = Float.parseFloat
(JOptionPane.showInputDialog(“Enter grade in 1st exam: “));
grade2= Float.parseFloat
(JOptionPane.showInputDialog(“Enter grade in 2nd exam: “));
grade3 = Float.parseFloat
(JOptionPane.showInputDialog(“Enter grade in 3rd exam: “));

float ave= (grade1+grade2+grade3)/3;
float round = Round(ave,2);

if(ave>=60){
JOptionPane.showMessageDialog(null,”Average: “+round+” :)”);
}
else{
JOptionPane.showMessageDialog(null,”Average: “+round+” :(“);
}
}

public static float Round(float Rval, int Rpl) {
float p = (float)Math.pow(10,Rpl);
Rval = Rval * p;
float tmp = Math.round(Rval);
return (float)tmp/p;
}

}

Getting Input, Conditional Constructs, Array, Command Line

Using Buffered Reader and JOptionPane, write a program that asks the user to input his or her name, age, address, telephone number and desired grade. Afterwards, print all the information provided by the user

//Buffered Reader
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Exercise3A {

public static void main(String[] args){
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));

String name=””;
String address=””;

int age=0;
int tele=0;
float grade=0;

try{
System.out.print(“Enter your name: “);
name= dataIn.readLine();

System.out.print(“Enter your age: “);
age = Integer.parseInt(dataIn.readLine());

System.out.print(“Enter address: “);
address= dataIn.readLine();

System.out.print(“Telephone number: “);
tele = Integer.parseInt(dataIn.readLine());

System.out.print(“Enter your desired grade: “);
grade = Float.parseFloat(dataIn.readLine());
}
catch(IOException e){
System.out.println(“Error.”);
}

System.out.println(“Name: “+name);
System.out.println(“Age: “+age);
System.out.println(“Address: “+address);
System.out.println(“Telephone number: “+tele);
System.out.println(“Desired Grade:”+grade);

}

}

//JOptionPane

import javax.swing.JOptionPane;

public class Exercise3B {

public static void main(String[] args){
String name=””;
String address=””;

int age=0;
int tele=0;
float grade=0;

name = JOptionPane.showInputDialog(“Enter your name: “);
age = Integer.parseInt
(JOptionPane.showInputDialog(“Enter your age: “));
address = JOptionPane.showInputDialog(“Enter address: “);
tele = Integer.parseInt
(JOptionPane.showInputDialog(“Phone number: “));
grade = Float.parseFloat
(JOptionPane.showInputDialog(“Enter your desired grade: “));

JOptionPane.showMessageDialog(null,
“\nName: “+name+
“\nAge: “+age+
“\nAddress: “+address+
“\nPhone number: “+tele+
“\nDesired Grade: “+grade);

}

}

Getting Input, Conditional Constructs, Array, Command Line

Using BufferedReader and JoptionPane, make the program to ask the following question
– Please enter your age
– Display the entered age as following
– If the age is over 100, display
*Hello , you are old!
Otherwise
*Hello , you are young!

//BufferedReader

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Exercise2A {

public static void main(String[] args){

String name=””;
int age=0;

BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));

try{
System.out.print(“Please enter your name: “);
name= dataIn.readLine();

System.out.print(“Please enter your age: “);
age = Integer.parseInt(dataIn.readLine());
}
catch(IOException e){
System.out.println(“Error.”);
}

if (age<=100){
System.out.println("\n"+"Hello"+" "+name+", "+"you are young"+".");
}
else{
System.out.println("\n"+"Hello"+" "+name+", "+"you are old"+".");
}

}

}

//JoptionPane

import javax.swing.JOptionPane;

public class Exercise2B {

public static void main(String[] args){
String name="";
int age=0;

name = JOptionPane.showInputDialog("Enter your name: ");
age = Integer.parseInt
(JOptionPane.showInputDialog("Enter yout age: "));

if (age<=100){
JOptionPane.showMessageDialog(null,
"\n"+"Hello"+" "+name+", "+"you are young"+".");
}
else{
JOptionPane.showMessageDialog(null,
"\n"+"Hello"+" "+name+", "+"you are old"+".");
}

}

}

Getting Input, Conditional Constructs, Array, Command Line

Using BufferedReader and JOptionPane, ask for three words from the user and output three words on the screen
ex.
Enter word1: Goodbye
Enter word2: and
Enter word3: Hello
——————————————————————
//BufferedReader
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Exercise1A {

public static void main(String[] args){
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
String word1=””;
String word2=””;
String word3=””;

try{
System.out.print(“Enter first word: “);
word1= dataIn.readLine();

System.out.print(“Enter second word: “);
word2= dataIn.readLine();

System.out.print(“Enter third word: “);
word3= dataIn.readLine();

}
catch(IOException e){
System.out.println(“Error.”);
}

System.out.println(“\n”+word1+” “+word2+” “+word3+”.”);
}

}

//JOptionPane
import javax.swing.JOptionPane;

public class Exercise1B {

public static void main(String[] args){
String word1=””;
String word2=””;
String word3=””;

word1 = JOptionPane.showInputDialog(“Enter first word: “);
word2 = JOptionPane.showInputDialog(“Enter second word: “);
word3 = JOptionPane.showInputDialog(“Enter third word: “);

String msg=word1+” “+word2+” “+word3+”.”;
JOptionPane.showMessageDialog(null, msg);
}

}

Javadavadoo!

JAVA NOTES

http://www.otherwise.com/Lessons/Introducton.html
http://www.leepoint.net/notes-java/index.html
http://www.roseindia.net/java/beginners/RoundTwoDecimalPlaces.shtml

SOURCECODES

http://www.roseindia.net/java/string-examples/java-count-vowels.shtml

http://www.java.happycodings.com/index.html
http://www.planet-source-code.com/vb/default.asp?lngWId=2
http://www.home.hs-karlsruhe.de/~pach0003/informatik_1/aufgaben/en/flowcontrol.html#sort_three_numbers

ARRAY
http://www.javapassion.com/handsonlabs/javaarray/index.html

OBECT ORIENTED PROGRAMMING SLIDES
http://www.dcs.bbk.ac.uk/~keith/oodp09/notes/

CLASS DECIMAL FORMAT
http://java.sun.com/j2se/1.4.2/docs/api/java/text/DecimalFormat.html

VIDEO TUTORIALS

http://www.20papercups.net/category/java-vtm-tutorials/

Hello world!

Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!

Follow

Get every new post delivered to your Inbox.