//Back slash (\) skips a line
public class grades
{
public static void main (String [] args)
{
int g1=90;
int g2=89;
int g3=85;
int total=g1+g2+g3;
int average=total/3;
System.out.println("The grade average is " + average);
}
}
_________________________________________________________________
If statement to output
import javax.swing.JOptionPane;
import java.util.Scanner;
public class average
{
public static void main (String [] args)
{
Scanner input = new Scanner (System.in);
int average;
System.out.println("Enter your grade average:");
average = input.nextInt();
if (average >= 90 && average <= 100){
System.out.println("You got an A!");
}
if (average >=80 && average <=89){
System.out.println("You got a B!");
}
if (average >=70 && average <=79){
System.out.println("You got a C!");
}
}
}
______________________________________________________________
import java.util.Scanner;
public class PrintHello
{
public static void main (String [] args)
{
Scanner input = new Scanner (System.in);
int count = 1, limit;
limit = input.nextInt();
while (count <= limit) {
count = count + 1;
}
}
}
_________________________________________________
Text Box & Response
//Write a java program to display a box where you type your age
import javax.swing.JOptionPane;
public class PrintAge
{
public static void main (String [] args)
{
JOptionPane.showMessageDialog(null, "What's your age?");
String age;
age = JOptionPane.showInputDialog( "Type your age");
JOptionPane.showMessageDialog(null, "Your age is " + age + ", Is that correct?");
}
}
__________________________________________________
_____________________________________________________________________
Text Box Pop up
//Write a java program to display Hello Java with a dialog box
import javax.swing.JOptionPane;
public class PrintMessageGUI
{
public static void main (String [] args)
{
JOptionPane.showMessageDialog(null, "Hello Java");
JOptionPane.showMessageDialog(null, "My name is Justin");
}
}
__________________________________________________________________
New class converting string to integer
//Write a java program to display a box where you type your age
import javax.swing.JOptionPane;
public class PrintAge
{
public static void main (String [] args)
{
String name;
String ageString;
int ageInt;
JOptionPane.showMessageDialog(null, "What's your age?");
age = JOptionPane.showInputDialog( "Type your age");
ageInt = Integer.parseInt(ageString);
JOptionPane.showMessageDialog(null, "Your age is " + ageString + "/nNext year your will be ") + (ageInt+1));
}
_______________________________________________________________
Take a letter from a word
//do char (name) by strings, and do (name) = ~the variable~
// charAt(#)
import javax.swing.JOptionPane;
import java.util.Scanner;
public class Info
{
public static void main (String [] args)
{
Scanner key = new Scanner (System.in);
String first;
String middle;
char mName;
String last;
System.out.println("Enter your first name:");
first = key.nextLine();
System.out.println("Enter your middle name:");
middle = key.nextLine();
mName = middle.charAt(0);
System.out.println("Enter your last name:");
last = key.nextLine();
JOptionPane.showMessageDialog(null,"My name is " + first + " " + mName + ". " + last);
}
}
If Statements
import javax.swing.JOptionPane;
import java.util.Scanner;
public class IfStatements
{
public static void main (String [] args)
{
Scanner input = new Scanner (System.in);
int grade1, grade2,
double average;
System.out.println("Enter grade 1");
grade1 = input.nextInt();
System.out.println("Enter grade 2");
grade2 = input.nextInt();
average = (grade1 + grade2)/2;
if (average >= 60){
System.out.println("You Passed with an " + average + "!");
JOptionPane.showMessageDialog(null,"You Passed with an " + average + "!");
}
else {
System.out.println("You Failed");
JOptionPane.showMessageDialog(null,"You Fail :(");
}
}
}
___________________________________________________
Time Calculator
import java.util.Scanner;
public class TimeCalculator
{
public static void main (String [] args)
{
Scanner input = new Scanner (System.in);
int seconds, minutes, hours, days, minute, hour, day;
System.out.println("Enter a number of seconds");
seconds = input.nextInt();
minutes=seconds/60;
hours=seconds/3600;
days=seconds/86400;
if (seconds >= 60){
System.out.println(" There are " + minutes + " minutes in that many seconds");
}
if (seconds >= 3600) {
System.out.println(" There are " + hours + " hours in that many seconds");
}
if (seconds >= 86400){
System.out.println(" There are " + days + " days in that many seconds");
}
}
}
import java.util.Scanner;
public class students
{
public static void main (String args [] )
{
Scanner input = new Scanner (System.in);
int amount, students = 1;
String name;
System.out.println ("How many students are in the class?");
amount = input.nextInt();
while (students <= amount)
{
System.out.println("What is your name");
name = input.nextLine();
input.nextLine();
System.out.println("Hello " + name);
students = students + 1;
}
}
}