[Java] My Progress 02-25-2013, 11:47 AM
#1
Hello HC, I am just posting this to 1: show off my progress that I make in java, and 2: so other beginners can read it and better themselves. I will post the objective/assignment that I have completed, and the source for it. This will be updated when I have made new programs. So here it goes.
My first program as you all probably saw was a super simple CLI calculator. Here is the code for it:
This next one is a program that takes your input and returns it to you until you enter "exit". Code:
This one is another simple program that takes a number input from you and counts from 0 to your number.
This program takes the users input and makes a staircase with the number of steps as the user inputed.
Here is a program that encodes your message (By encoding I mean moving the character up one step in the ASCII chart.)
This one just takes the input (age) of the user, and the input (age) of the computer and tells you how much older you/it are compared to the other.
Computer price depreciation - This program basicly takes your input on how much the computer costs new, your input on how old it is, and your rate of depreciation and calculates how much it is worth now.
Ask the user for a string - The program shall sum up the ASCII values of each character of the input string and print the sum.
My first program as you all probably saw was a super simple CLI calculator. Here is the code for it:
Code:
import java.util.Scanner;
public class calculator {
public static void main(String args[]) {
Scanner lightx = new Scanner(System.in);
double fnum, snum, answer = 0, operation;
System.out.println("Enter your first number: ");
fnum = lightx.nextDouble();
System.out.println("Enter your second number: ");
snum = lightx.nextDouble();
System.out.println("What operation would you like to use? (+ = 1, - = 2, * = 3, / = 4)");
operation = lightx.nextDouble();
if (operation == 1){
answer = fnum + snum;
}
if (operation == 2){
answer = fnum - snum;
}
if (operation == 3){
answer = fnum * snum;
}
if (operation == 4){
answer = fnum / snum;
}
System.out.println(answer);
}
}
This next one is a program that takes your input and returns it to you until you enter "exit". Code:
Code:
import java.util.Scanner;
public class Repeat {
public static void main(String args[]){
Scanner word = new Scanner(System.in);
String word1;
do {
System.out.println("Input: ");
word1 = word.nextLine();
if(!word1.equals("exit")){
System.out.println(word1);
}
} while(!word1.equals("exit"));
}
}
This one is another simple program that takes a number input from you and counts from 0 to your number.
Code:
import java.util.Scanner;
public class LightX {
public static void main(String args[]){
Scanner number = new Scanner(System.in);
int lightx = 0;
System.out.println("What would you like the number to end on?");
int num = number.nextInt();
while (lightx <= num){
System.out.println(lightx);
lightx++;
}
}
}
This program takes the users input and makes a staircase with the number of steps as the user inputed.
Code:
import java.util.Scanner;
public class Stair {
public static void main(String args[]) throws Exception {
Scanner s = new Scanner(System.in);
System.out.println("Number of stairs: ");
int maxLines = s.nextInt();
String stair = "";
for(int n = maxLines; n > 0; n--) {
stair += "*";
System.out.println(stair);
}
}
}
Here is a program that encodes your message (By encoding I mean moving the character up one step in the ASCII chart.)
Code:
import java.util.Scanner;
public class Encode {
public static void main(String args[]){
Scanner s = new Scanner(System.in);
System.out.print("Input goes here: ");
String word = s.nextLine();
for(char a: word.toCharArray()){
int w = a;
w++;
System.out.print(Character.toChars(w));
}
}
}
This one just takes the input (age) of the user, and the input (age) of the computer and tells you how much older you/it are compared to the other.
Code:
import java.util.Scanner;
public class Comp {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
int userAge,compAge = 0;
System.out.println("How old are you?");
userAge = s.nextInt();
System.out.println("How old is your computer?");
compAge = s.nextInt();
if(compAge > userAge){
System.out.println("Your computer is " + (compAge - userAge) + " years older than you. Time for a new one.");
} else {
System.out.println("You are " + (userAge - compAge) + " years older than your computer.");
}
}
}
Computer price depreciation - This program basicly takes your input on how much the computer costs new, your input on how old it is, and your rate of depreciation and calculates how much it is worth now.
Code:
import java.util.Scanner;
public class Price {
public static void main(String args[]){
Scanner s = new Scanner(System.in);
int price,age,d = 0;
System.out.println("How much did your computer cost new?");
price = s.nextInt();
System.out.println("How old is your computer? (In years)");
age = s.nextInt();
System.out.println("What is your depreciation rate?");
d = s.nextInt();
for(int compAge = age; compAge > 0; compAge--) {
price /= d;
}
System.out.println("Your computer is worth $" + price);
}
}
Ask the user for a string - The program shall sum up the ASCII values of each character of the input string and print the sum.
Code:
import java.util.Scanner;
public class Encode {
public static void main(String args[]){
Scanner s = new Scanner(System.in);
System.out.print("Input goes here: ");
String word = s.nextLine();
int w = 0;
for(char a: word.toCharArray()){
w += a;
}
System.out.println(w);
}
}