import java.util.Scanner;
/* AniBlogger12
* Methods
*/
public class methods7 {
// method for returning fibonacci sequence
public static int fibonacci(int num) {
// if the number is 0, return 0
if (num == 0) {
return 0;
// or else if the number is 1, return 1
} else if (num == 1) {
return 1;
// else return the sum of the 2 last numbers to get the next term in the
// sequence
} else {
return fibonacci(num - 1) + fibonacci(num - 2);
}
}
// main
public static void main(String[] args) {
// get keyboard
Scanner kb = new Scanner(System.in);
// prompt user
System.out.println("Please enter the number of terms you want to display the fibonacci series:");
// user input
int num = kb.nextInt();
// result variable
int result = 0;
// for loop
for (int i = 0; i < num; i++) {
// result is equal to the fibonacci of (i)
result = fibonacci(i);
// print out result
System.out.println(result);
}
}
}
Category: Java
Binary Addition
import java.util.Scanner;
/*AniBlogger12
* Binary Addition Program
*/
public class binaryAddition {
public static void main(String[] args) {
// get keyboard
Scanner keyboard = new Scanner(System.in);
// prompt user
System.out.println("Enter a binary number.");
// user integer number input
int num1 = keyboard.nextInt();
// convert the int number to a string
String number1 = Integer.toString(num1);
// make length variable and set it equal to the length of the number entered
int length1 = number1.length();
// prompt user asking for another binary number
System.out.println("Enter another binary number.");
// user input for another binary number
int num2 = keyboard.nextInt();
// convert the int number to a string
String number2 = Integer.toString(num2);
// make length variable and set it equal to the length of the number entered
int length2 = number2.length();
// add the two binary numbers like adding normal numbers
int add = num1 + num2;
// convert sum to a string (this is not the answer)
String notanswer = Integer.toString(add);
// add a zero to the string variable
notanswer = "0" + notanswer;
// find length of this
int length = notanswer.length();
// make a string digit variable
String digit = "";
// make an answer variable
String answer = "";
// make and initialize a carry variable
int carry = 0;
// if the length of the first num is greater than the length of the second num
if (length1 > length2) {
// for loop
// iterate forward
for (int i = 0; i <= length1; i++) {
// take part of the digit from length - i - 1th position to length -ith position
digit = notanswer.substring(length - i - 1, length - i);
// if first digit is 0 and there is nothing to carry over
if (digit.equals("0") && carry == 0) {
// add a 0 to the answer
answer = "0" + answer;
// carry stays 0
carry = 0;
}
// or else if the first digit is 0 and carry is 1
else if (digit.equals("0") && carry == 1) {
// add 1 to the answer
answer = "1" + answer;
// carry becomes 0
carry = 0;
}
// or else if the first digit is 1 and carry is 0
else if (digit.equals("1") && carry == 0) {
// add 1 to the answer
answer = "1" + answer;
// carry over 0
carry = 0;
}
// else if the first digit is 1 and carry is 1
else if (digit.equals("1") && carry == 1) {
// add 0 to answer
answer = "0" + answer;
// carry over 1
carry = 1;
}
// else if the first digit is 2 and carry is 0
else if (digit.equals("2") && carry == 0) {
// add 0
answer = "0" + answer;
// carry over 1
carry = 1;
}
// else if the first digit is 2 and carry is 1
else if (digit.equals("2") && carry == 1) {
// add 1 to answer
answer = "1" + answer;
// carry over 1
carry = 1;
}
}
}
// or else
// if first number is smaller than second number
else if (length1 < length2) {
// for-loop
for (int i = 0; i <= length2; i++) {
// same as last time
digit = notanswer.substring(length - i - 1, length - i);
// same as before
if (digit.equals("0") && carry == 0) {
answer = "0" + answer;
carry = 0;
}
else if (digit.equals("0") && carry == 1) {
answer = "1" + answer;
carry = 0;
}
else if (digit.equals("1") && carry == 0) {
answer = "1" + answer;
carry = 0;
}
else if (digit.equals("1") && carry == 1) {
answer = "0" + answer;
carry = 1;
}
else if (digit.equals("2") && carry == 0) {
answer = "0" + answer;
carry = 1;
}
else if (digit.equals("2") && carry == 1) {
answer = "1" + answer;
carry = 1;
}
}
}
// or else if both are equal
else if (length1 == length2) {
// for-loop
for (int i = 0; i <= length2; i++) {
// same as before
digit = notanswer.substring(length - i - 1, length - i);
if (digit.equals("0") && carry == 0) {
answer = "0" + answer;
carry = 0;
}
else if (digit.equals("0") && carry == 1) {
answer = "1" + answer;
carry = 0;
}
else if (digit.equals("1") && carry == 0) {
answer = "1" + answer;
carry = 0;
}
else if (digit.equals("1") && carry == 1) {
answer = "0" + answer;
carry = 1;
}
else if (digit.equals("2") && carry == 0) {
answer = "0" + answer;
carry = 1;
}
else if (digit.equals("2") && carry == 1) {
answer = "1" + answer;
carry = 1;
}
}
}
// print out the final sum
System.out.println(answer);
}
}
Pizza Order – If-Else Statements & Switch Statements
import java.util.Scanner;
/*AniBlogger12
* Pizza Order
*/
public class PO {
// main
public static void main(String[] args) {
// get keyboard for user input
Scanner kb = new Scanner(System.in);
// prompt user
System.out.println("Please enter the size of the pizza (12, 14, or 16 inch)");
// make size variable
double size = kb.nextDouble();
// prompt user asking for number of toppings
System.out.println("Please enter the number of toppings");
// user input for number of toppings
int toppings = kb.nextInt();
// deduct $5.50 away from toppings
double newtoppings = toppings - 5.5;
// toppings price variable
double tprice;
// switch statement
switch (toppings) {
// first case, no toppings price
case 0:
tprice = 0;
break;
// second case, $2
case 1:
tprice = 2;
break;
// third case, $3
case 2:
tprice = 3;
break;
// if not the above, price is $3 plus $.50 ^ # of toppings
default:
tprice = 3 + Math.pow(.50, newtoppings);
}
// pizza prices for size 12, 14, and 16 pizzas
double price1 = 9.00;
double price2 = 11.00;
double price3 = 14.00;
// if the pizza size is 12, add $9.00 to the toppings price and print out
if (size == 12) {
price1 += tprice;
System.out.println("The cost of the pizza is " + price1);
}
// if the pizza size is 14, add $11.00 to the toppings price and print out
if (size == 14) {
price2 += tprice;
System.out.println("The cost of the pizza is " + price2);
}
// if the pizza size is 16, add $14.00 to the toppings price and print out
if (size == 16) {
price3 += tprice;
System.out.printf("The cost of the pizza is " + price3);
}
}
}
Odds and Evens – Arrays
import java.util.Arrays;
import java.util.Scanner;
/*AniBlogger12
* Odds and Evens- Arrays
*/
public class oddsAndEvens {
public static void main(String[] args) {
// get keyboard
Scanner kb = new Scanner(System.in);
// initialize array, specify number of integers you want typed
int[] array = new int[10];
// prompt user
System.out.println("Please enter a list");
// for 10 numbers entered
for (int i = 0; i < 10; i++) {
// put it into an array
array[i] = kb.nextInt();
}
// loop through the numbers one by one
for (int i = 0; i < array.length; i++) {
//if number is divisible by 2
if (array[i] % 2 == 0) {
//it is even and is replaced with 0
array[i] = 0;
//or else:
} else {
//it is odd and is replaced with 1
array[i] = 1;
}
}
//print out the new array
System.out.print("The new array is " + Arrays.toString(array) + "\n");
}
}
Conversion of Time
import java.util.Scanner;
/* AniBlogger12
* Program to convert elapsed time in the form of hours, minutes, seconds to seconds.
*/
public class TP {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter the time as three integers: hours, minutes, and seconds");
// Define hours, minutes, and seconds
int hours = keyboard.nextInt();
int minutes = keyboard.nextInt();
int seconds = keyboard.nextInt();
//Convert hours to seconds: hours*3600= seconds
//Convert minutes to seconds: minutes*60=seconds
//Add hours+ minutes+seconds to get total seconds
int total = hours * 3600 + minutes * 60 + seconds;
System.out.println(hours + ":" + minutes + ":" + seconds + " elapsed time converts to " + total + " seconds.");
}
}
Program to Find the Average of 5 Grades
import java.util.Scanner;
/* AniBlogger12
* Finding the letter grade based on the percent grade entered
*/
public class AGRP {
public static void main(String[] args) {
System.out.println("Please enter 5 of your grades in integers:");
Scanner kb = new Scanner(System.in);
// 5 grades inputed
int g1 = kb.nextInt();
int g2 = kb.nextInt();
int g3 = kb.nextInt();
int g4 = kb.nextInt();
int g5 = kb.nextInt();
//average grade
double grade = (g1 + g2 + g3 + g4 + g5) / 5;
//grade rounded to nearest integer: use Math.rint and cast it to make it an int.
int rgrade = (int) Math.rint(grade);
// grade rounded and printed out to the nearest 10th, use %.1f.%n to round it one decimal place to the left
System.out.printf("The grade rounded to the nearest tenth is %.1f.%n", grade);
// If the grade is greater than or equal to(>=) 90, the grade is an A.
if (Math.rint(rgrade) >= 90) {
System.out.println("The grade is an A.");
}
// If it isn't, then if the grade is greater than or equal to(>=) 80
else if (rgrade >= 80 ) {
System.out.println("The grade is a B.");
}
//If it isn't, then if the grade is greater than or equal to(>=) 70
else if (rgrade >= 70 ) {
System.out.println("The grade is a C.");
}
//If it isn't, then if the grade is greater than or equal to(>=) 60
else if (rgrade >= 60 ) {
System.out.println("The grade is a D.");
}
//If it isn't, then if the grade is less than(<) 60, it is an F.
else if (rgrade < 60) {
System.out.println("The grade is an F.");
}
}
}
Hello, World! (Java)
“Hello, World!” is the first program written by most programmers.
If you are anywhere from being a beginner to being more experienced in Java, and if you have Windows or Mac, I recommend installing Eclipse IDE here. If you do not have Windows or Mac, I recommend creating an account on Repl.it here.
Eclipse:
Once you download Eclipse, go to:
File–>New–>Java Project–>(Name the Project)–> FINISH.

Then, right click on the file and click New–>Class–>(Name the Class).

When you name the class, be sure to not put any characters or numbers as the beginning of the name. Also, do not include any spaces in the name. Finally, select the public static void main(String [] args) option and press FINISH. Now you are all set! 🙂

Here’s how it works:
Input:
// This is a comment in Java, it does not affect the program or its output
// System.out.println() is used to print something out in java and go to the next line
// Quotes are used to denote that we are printing out a string
// You must put semicolons at the end of each line (except for comments, of course)
System.out.println("Hello, World!");
Output:
Hello, World
In Java, only double quotes (” “) are allowed for Strings. Therefore if you want to print something along with double quotes in java, you would have to do this:
Input:
// print something out with double quotes in java
System.out.println("\"Hello, World!\"");
Output:
"Hello, World!"