1. arrays/print
public static void print(int[] a) {
for (int i = 0; i <a.length; i++) {
System.
out.
println("element " + "[" + i
+ "]" + " is " + a
[i
]);
}
}
2. arrays/maxValue
public static int maxValue(int[] a) {
for (int i = 0; i <a.length; i++) {
if (a[i] > max) {
max = a[i];
}
}
return max;
}
3. arrays.squareArray
int[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] squares = new int[array.length];
for (int i = 0; i < array.length; i++) {
squares[i] = array[i] * array[i];
}
4. console output/printProgram
public class PrintProgram{
//static String s = "public class public class Hello {";
public static void main
(String[] args
) {
System.
out.
println("public class Hello {");
System.
out.
println(" public static void main(String[] args) {");
System.
out.
println(" System.out.println(\"Hello, world!\");");
}
}
5. consile output/calculate line
import java.util.Scanner;
public class CalculateLine {
public static void main
(String [] args
) {
int x;
int y;
Scanner odczyt
= new Scanner
(System.
in);
System.
out.
println("This program calculates y coordinates for a line.");
System.
out.
print("Enter slope (m): ");
int slope = odczyt.nextInt();
System.
out.
print("Enter intercept (b): ");
int intercept = odczyt.nextInt();
System.
out.
print("Enter x: ");
x = odczyt.nextInt();
while (x != -1) {
y = slope*x + intercept;
System.
out.
println("f("+ x
+") = " + y
);
System.
out.
print("Enter x: ");
x = odczyt.nextInt();
}
}
}
6. cons out/evensumMax
import java.util.Scanner;
public static void evenSum() {
int quantity;
int integer;
int max = -1000000;
int evenSum = 0;
Scanner odczyt
= new Scanner
(System.
in);
System.
out.
print("how many integers? ");
quantity = odczyt.nextInt();
for (int i = 0 ; i < quantity; i++) {
System.
out.
print("next integer? ");
integer = odczyt.nextInt();
if (integer%2 == 0) {
evenSum += integer;
if (integer > max) {
max = integer;
}
}
}
System.
out.
println("even sum = " + evenSum
);
System.
out.
println("even max = " + max
);
}
7. cons out/FearTheTree
System.
out.
println("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\");
System.
out.
println("|| FEAR THE TREE! ||");
System.
out.
println("////////////////////");
8. cons out/inches to cm
import java.util.Scanner;
import java.text.*;
public class InchesToCentimeters{
static public double round(double d, int ic) {
nf.setMaximumFractionDigits(ic);
nf.setMinimumFractionDigits(ic);
return Double.
parseDouble((nf.
format(d
)).
replaceAll(",",
".").
replaceAll(" ",
"") );
}
public static void main
(String[] args
) {
System.
out.
println("This program converts feet and inches to centimeters.");
int Feet;
int Inches;
double Centimeters, Cm_Format;
Scanner sc
= new Scanner
(System.
in);
System.
out.
print("Enter number of feet: ");
Feet = sc.nextInt();
//int feet = Integer.parseInt(Feet);
System.
out.
print("Enter number of inches: ");
Inches = sc.nextInt();
//Double inches = Double.parseDouble(Inches);
Feet_Inches = Inches / 12.00 ;
Suma = Feet + Feet_Inches;
//Double suma = Double.parseDouble(Suma);
Centimeters = Suma * 30.48;
System.
out.
print(Feet
+ "ft " + Inches
+ "in " + "= " + round
(Centimeters,
2) + "cm");
}
}
9. cons/out/number square
public static void numberSquare(int min, int max) {
int range = max - min + 1;
for (int i = 0; i < range; i ++) {
for(int j = 0; j < range; j++ ) {
System.
out.
print( (i
+ j
) % (range
) + min
);
}
}
}
10. cons out/print triangle
public static void printTriangle() {
for(int i=0;i<=5; i++) {
for (int j=0;j<=i;j++){
}
}
}
11. cons out/stanford vs call
import java.util.Scanner;
public class StanfordVsCal{
public static void main
(String[] args
) {
int Stanford;
int Cal;
Scanner odczyt
= new Scanner
(System.
in);
System.
out.
print("Stanford: How many points did they score? ");
Stanford = odczyt.nextInt();
System.
out.
print("Cal: How many points did they score? ");
Cal = odczyt.nextInt();
if (Stanford > Cal) {
System.
out.
println("Stanford won!");
}else {
System.
out.
println("Cal won!");
}
}
}
12. expressions / digit expressions
10s place: number%100/10
100s place: number%1000/100
13. expressions/displacement
import java.lang.Math;
double s0 = 12.0;
double v0 = 3.5;
double a = 9.8;
double t = 10.0;
double s
= s0
+ v0
*t
+ 0.5 * a
* Math.
pow(10,
2);
14. expressions/printMaxMin
46,36,23,13
15.expresions/times operator
double y;
y = x*(x*(x*(12.3*x - 9.1) + 19.3) - 4.6) + 34.2;
16. expressions/valuesOfABC
6,9,16
17. expressions/valuesOfIJK
4,2,1
18. loops/biggerAndSmaller
import java.util.Scanner;
public class BiggestAndSmallest {
public static void main
(String[] args
){
Scanner sc
= new Scanner
(System.
in);
System.
out.
print("Number of numbers? ");
int n = sc.nextInt();
int biggest = -100000000;
int smallest = 10000000;
for (int i = 1; i <=n; i++) {
System.
out.
print("Number " + i
+": ");
int number = sc.nextInt();
if (number>biggest) {
biggest = number;
}
if (number < smallest) {
smallest = number;
}
}
System.
out.
println("Biggest = " + biggest
);
System.
out.
println("Smallest = " + smallest
);
}
}
19. loops/ComputeSumOfDigits
import java.util.Scanner;
public class ComputeSumOfDigits {
public static void main
(String[] args
){
int integer, Int;
int suma = 0;
Scanner odczyt
= new Scanner
(System.
in);
System.
out.
print("Type an integer: ");
integer = odczyt.nextInt();
int length
= (int)(Math.
log10(integer
)+1);
while (integer > 0) {
suma += integer%10;
integer = integer/10;
//System.out.println("Digit sum is: ");
}
System.
out.
println("Digit sum is " + suma
);
}
}
20. loops/FizzBuzz
import java.util.Scanner;
public class FizzBuzz {
public static void main
(String[] args
) {
int max;
double a;
Scanner odczyt
= new Scanner
(System.
in);
System.
out.
print("Max value? ");
max = odczyt.nextInt();
a = max / 20.0;
for (int i = 0; i < a; i++) {
for (int j = 0; j < 20; j++) {
if ((i*20 + j + 1)%15==0) {
System.
out.
print("FizzBuzz ");
} else if ((i*20 + j + 1)%5==0) {
}else if ((i*20 + j + 1)%3==0) {
} else {
System.
out.
print(i
*20 + j
+ 1);
}
if ((i*20 + j + 1)==max ) {
break;
}
}
}
}
}
21. loops/numberLoops1
for (int i = 1; i<=5; i++){
for (int j = 1; j <=i; j++) {
print(i);
}
println();
}
22. loops/numberLoops2
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j ++) {
if (j < 6-i){
print(".");
}
else {
print(i);
}
}
println();
}
23. loops/numberLoops3
for (int i = 1; i <= 5; i++){
for (int j = 5; j >= 1; j--) {
if (i == j) {
print(i);
}
else {
print(".");
}
}
println();
}
24. loops/numberSquare
import java.util.Scanner;
public class NumberSquare{
public static void main
(String[] args
) {
int min, max;
Scanner odczyt
= new Scanner
(System.
in);
min = odczyt.nextInt();
max = odczyt.nextInt();
int range = max - min + 1;
for (int i = 0; i < range; i ++) {
for(int j = 0; j < range; j++ ) {
System.
out.
print( (i
+ j
) % (range
) + min
);
}
}
}
}
25. loops/sentinelSum
import java.util.Scanner;
public class SentinelSum {
public static void main
(String[] args
) {
int number, suma=1;
Scanner sc
= new Scanner
(System.
in);
do {
System.
out.
print("Type a number: ");
number = sc.nextInt();
suma += number;
} while (number != -1);
System.
out.
print("Sum is " + suma
);
}
}
26. parameters/boxOfStars
public static void boxOfStars(int width, int hight){
for (int i = 0; i < hight; i++) {
for (int j = 0; j < width; j++) {
if (i == 0) {
} else if ((i > 0 && i < hight - 1) && (j == 0 || j == width -1)) {
} else if ( i == hight -1) {
}else {
}
}
}
}
27. parameters/countDigits
public static int countDigits
(int Number) {
return number.length();
} else{
return number.length() - 1;
}
}
28. parameters/investment
import java.util.Scanner;
public class Investment {
public static Double FinalAmount
(Double PV,
double r,
int n
) {
FV
= PV
*Math.
pow(1+r, n
);
return FV;
}
return FV - PV;
}
public static double CalcPercent(double PV, double Profit) {
return (Profit-PV) / PV* 100.00;
}
public static String Category
(double Percent
){
if (Percent >= 0 && Percent < 10) {
return "weak";
}else if (Percent >= 10 && Percent < 50) {
return "medium";
} else {
return "strong";
}
}
public static void Oblicz
(String inwestor, Scanner odczyt
) {
int n;
// Scanner odczyt = new Scanner(System.in);
System.
out.
println("Investor #" + inwestor
+ ":");
System.
out.
print("Initial amount? ");
PV = odczyt.nextDouble();
System.
out.
print("Interest rate%? ");
r = odczyt.nextDouble();
System.
out.
print("Num. of months? ");
n = odczyt.nextInt();
Double finalAmount
= FinalAmount
(PV, r, n
);
System.
out.
printf("Final amount = $%.2f\n", finalAmount
);
Double profit
= Profit
(PV, finalAmount
);
Double Percent
= CalcPercent
(PV, finalAmount
);
System.
out.
printf("Profit = $%.2f (%.0f%%)\n", profit, Percent
);
System.
out.
println(Category
(Percent
));
}
public static void main
(String[] args
) {
Scanner odczyt
= new Scanner
(System.
in);
Oblicz("1", odczyt);
Oblicz("2", odczyt);
System.
out.
println("Have a nice day!");
}
}
29. parameters/printpay
public static void printPay
(Double Salary,
int Hours
) {
int Difference;
if (Hours <= 8) {
Earnings = Salary * Hours;
} else {
Difference = Hours - 8;
Earnings = 1.5*Salary *Difference + 8*Salary;
}
System.
out.
println("Hours worked: " + Hours
);
System.
out.
printf("Pay earned: $" + String.
format( "%.2f", Earnings
) );
}
30. parameters/triangle
public static void triangle(int size){
for (int i = 0; i < size; i ++){
for (int j = 1; j <= size; j++) {
if (j < size - i) {
}else {
}
}
}
}
31. parameters/getDaysMonth
public static int getDaysInMonth(int Month) {
int [] Days = {31,28,31,30,31,30,31,31,30,31,30,31};
return Days[Month - 1];
}