All the code that I will post on this page will be from my JavaTestCode
I am going to start out with small code, then I will write and post more advanced code.
<========================>
This code is borrowed and I am editing a simple method, this is the way I re write code I copy and paste into notepad and I edit it clean the code and run the code in Eclipse and view on my console, if I get any errors then i debug the code
-------------------
// SampleMethods.java -- Shows loop examples inside methods.
// Author: Fred Swartz - 2005 Dec 11
package SampleMethod
import javax.swing.*;
import java.util.*;
public class SampleMethods {
//======================================================= primeFactor
// Returns a factor of n, or 0 if n is prime.
// Uses simple algorithm of dividing by all numbers up to n.
// Could improve efficiency many ways.
public static int primeFactor(int n) {
for (int divisor = 2; divisor < n; divisor++) {
if ((n % divisor) == 0) {
return divisor; // Divisible implies not prime!
}
}
return 0; // Must be prime if nothing was able to divide it.
}
//======================================================= countVowels
// Or could use substring, ||, switch, nested for loops, ...
public static int countVowels(String text) {
int vowelCount = 0;
for (int pos = 0; pos < text.length(); pos++) {
if ("aeiouAEIOU".indexOf(text.charAt(pos)) >= 0) {
vowelCount++;
}
}
return vowelCount;
}
//========================================================= summation
// Returns sum off all numbers from 1 to n.
// Uses loop instead of just returning (n * (n+1)) / 2
public static int summation(int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
return sum;
}
//========================================================== reverse
// Reverse string. Should use StringBuilder for efficiency.
public static String reverse(String s) {
String rev = "";
for (int pos = 0; pos < s.length(); pos++) {
rev = s.substring(pos, pos+1) + rev;
}
return rev;
}
//================================================= readAndAddDialog
public static int readAndAddDialog() {
int sum = 0;
while (true) { // Loop until Cancel or close box clicked.
String input = JOptionPane.showInputDialog(null, "Input");
if (input == null) {
break; //***************************** EXIT LOOP
}
int num = Integer.parseInt(input);
sum += num;
}
return sum;
}
//================================================= readAndAddScanner
public static int readAndAddScanner() {
Scanner in = new Scanner(System.in);
int sum = 0;
System.out.println("Enter numbers to sum. Non-number terminates.");
while (in.hasNextInt()) { // Loop until no integer
int num = in.nextInt();
sum += num;
}
return sum;
}
//=============================================================== main
public static void main(String[] args) {
assert primeFactor(2) == 0 : "primeFactor(2)";
assert primeFactor(4) != 0 : " primeFactor(4)";
assert primeFactor(112) != 0 : "primeFactor(112)";
assert primeFactor(113) == 0 : "primeFactor(113)";
assert countVowels("") == 0 : "countVowels(\"\")";
assert countVowels("a") == 1 : "countVowels(\"a\")";
assert countVowels("e") == 1 : "countVowels(\"e\")";
assert countVowels("i") == 1 : "countVowels(\"i\")";
assert countVowels("o") == 1 : "countVowels(\"o\")";
assert countVowels("u") == 1 : "countVowels(\"u\")";
assert countVowels("A") == 1 : "countVowels(\"A\")";
assert countVowels("E") == 1 : "countVowels(\"E\")";
assert countVowels("I") == 1 : "countVowels(\"I\")";
assert countVowels("O") == 1 : "countVowels(\"O\")";
assert countVowels("U") == 1 : "countVowels(\"U\")";
assert countVowels("x") == 0 : "countVowels(\"x\")";
assert countVowels("x") == 0 : "countVowels(\"x\")";
assert countVowels("nw s th tm fr...") == 0 : "countVowels(\"nw s th tm fr..\")";
assert countVowels("now is the time for...") == 6 : "countVowels(\"now is the time for...\")";
assert summation(1) == 1 : "summation(1)";
assert summation(2) == 3 : "summation(2)";
assert summation(3) == 6 : "summation(3)";
assert summation(4) == 10 : "summation(4)";
assert reverse("").equals("") : "reverse(\"\")";
assert reverse("a").equals("a") : "reverse(\"a\")";
assert reverse("ab").equals("ba") : "reverse(\"ab\")";
assert reverse("abc").equals("cba") : "reverse(\"abc\")";
int r1 = readAndAddDialog();
JOptionPane.showMessageDialog(null, "Sum is " + r1);
int r2 = readAndAddScanner();
System.out.println("Sum is " + r2);
}
}
<=============================================>
And from my joefirstclass
This Program is a small Switch Statement Code
package JavaJoeTestCode;
/**
* @author JavaJoeDeveloper
*
*/
public class joefirstclass {
/**
* @param args
*/
public static void main(String[] args) {
int age;
age= 15;
switch(age){
case 1:
System.out.println("You can crawl");
break;
case 2:
System.out.println("You can talk");
break;
case 3:
System.out.println("You can walk");
break;
default:
System.out.println("You can get into trouble");
break;
}
}
}
While Statement Sample Code:
/**
*
*/
package JavaJoeTestCode;
/**
* @author JavaJoeDeveloper
*
*/
public class joefirstclass {
/**
* @param args
*/
public static void main(String[] args) {
int counter = 0;
while (counter < 10) {
System.out.println (counter);
counter ++;
}
}
}
<=========================>
public static void main(String[] args {
int age = 60;
if (age < 50){
System.out.println("You are nice n young";
} else {
System.out.println("You are old");
}
}
}
<=================================>
/**
*
*/
package JavaJoeTestCode;
/**
* @author JavaJoeDeveloper
*
*/
import java.util.Scanner;
public class joefirstclass {
/**
* @param args
*/
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int total = 0;
int grade;
int average;
int counter = 0;
while(counter < 10);{
grade = input.nextInt();
total = total + grade;
counter++;
}
average = total/10;
System.out.println("Your average is " + average);
}
}
<==============>
Basic Calculator:
package javatestcoding;
import java.util.Scanner;
public class MyFirstClass {
public static void main(String[] args) {
Scanner Joseph = new Scanner(System.in);
double fnum, snum, answer;
System.out.println("Enter first num: ");
fnum = Joseph.nextDouble();
System.out.println("Enter second num: ");
snum = Joseph.nextDouble();
answer = fnum + snum;
System.out.println(answer);
}
}
<==================>
Java Joe's Small Array
package javatestcoding;
public class MyFirstClass {
public static void main(String[] args) {
int joseph [] = new int [10];
joseph[0]= 50;
joseph[1]= 250;
joseph[2]= 51;
joseph[3]= 52;
joseph[4]= 53;
joseph[5]= 54;
joseph[6]= 55;
joseph[7]= 57;
joseph[8]= 58;
joseph[9]= 59;
System.out.println(joseph [1]);
<========================>
Java Joe's Other Array
package javatestcoding;
public class MyFirstClass {
public static void main(String[] args) {
int joseph[]={2,4,6,8,10};
System.out.println(joseph [3]);
}
}