Java Examples I Fall 2010
Java Examples I Fall 2010
Java Examples I Fall 2010
Java Examples I
class ExTemplate {
public static void main (String args[]){
//
}
}
class Ex {
public static void main (String args[]){
int i,j,k;
i=3;
j=5;
k=i+j;
System.out.println (" k = "+k);
}
}
class Ex1 {
public static void main (String args[]){
int a,b;
// if a > b, print "a > b"
// else if a < b, print "a > b"
a=3;
b=5;
if (a > b)
System.out.println ("a > b");
else
System.out.println (" a < b");
}
}
class Ex2 {
public static void main (String args[]){
int a=4;
int b=5;
int c=3;
c= (a * b) - (b * c)/a;
System.out.println ("c = " +c);
}
}
class Ex3 {
public static void main (String args[]){
String s ="CSCI 120";
System.out.println ("s = " +s);
}
}
class Ex4 {
public static void main (String args[]){
float f=3.14f;
System.out.println ("f = " +f);
}
}
class Ex5 {
public static void main (String args[]){
char c= 'N';
System.out.println ("c = " +c);
2
}
}
import java.util.*;
class Ex6{
public static void main (String args[]){
import java.util.*;
class Ex7{
public static void main (String args[]){
import java.util.*;
class Ex9{
public static void main (String args[]){
import java.util.*;
class Ex10{
public static void main (String args[]){
System.out.println ("Enter a string!");
import java.util.*;
public class Ex11 { // Scanner class
public static void main(String[] args ) {
double price, tax;
Scanner sc = new Scanner(System.in);
System.out.println ("*** Product Price Check ***");
System.out.print ("Enter initial price: ");
price = sc.nextDouble();
System.out.print("Enter tax rate: ");
tax = sc.nextDouble();
if (price > 100) {
System.out.println ("Special Promotion: Your tax will be halved! ");
tax = tax * 0.5;
}
price = price * (1 + tax/100);
System.out.println("Cost after tax = " + price);
}
}
public class Ex12 { // For loop
public static void main(String[] args ) {
for (int i = 1; i <= 5; i++) {
System.out.println("*****");
}
}
}
public class Ex13 { // For loop
public static void main(String[] args ) {
for (int i = 1; i <= 5; i++) {
System.out.println("i=" + i);
}
}
}
public class Ex14 { // For loop
public static void main(String[] args ) {
int k=0;
for (int i = 1; i <= 5; i++) {
System.out.print("i=" + i + " " + " & ");
k=k+i;
System.out.println("k=" + k);
}
}
}
public class E15 { //count down
public static void main(String[] args) {
for (int i=10; i >= 1; i--) {
System.out.println(i);
}
}
}
import java.util.*;
class Ex17 {
public static void main(String[] args) {
int mark;
Scanner sc = new Scanner(System.in);
System.out.println ("What exam mark did you get?");
mark = sc.nextInt();
while (mark < 0 || mark > 100) { //while loop
System.out.println ("invalid mark: Re-enter!");
mark = sc.nextInt();
}
if ( mark>=96 && mark <=100)
System.out.println ("A grade");
else if ( mark>=91 && mark <=95)
System.out.println ("A- grade");
else if ( mark>=87 && mark <=90)
System.out.println ("B+grade");
else if ( mark>=83 && mark <=86)
System.out.println ("B grade");
else if ( mark>=79 && mark <=82)
System.out.println ("B- grade");
else if ( mark>=75 && mark <=78)
System.out.println ("C+ grade");
else if ( mark<75)
System.out.println ("Not a good CSCI 120 grade");
}
}
import java.util.*;
public class Ex18 {
public static void main(String[] args ) {
int i=10;
do {
i=i-1;
System.out.println ("i= " +i);
} while (i>0);
}
}
import java.util.*;
public class Ex19 {
public static void main(String[] args ) {
int i=10;
do {
i=i-1;
System.out.println ("i= " +i);
} while (i>0);
}
}
import java.util.*;
class Ex20 {
public static void main(String[] args) {
Scanner sc =new Scanner (System.in);
int day = sc.nextInt();
switch (day) {
case 1: System.out.println("Sunday"); break;
case 2: System.out.println("Monday"); break;
case 3: System.out.println("Tuesday"); break;
case 4: System.out.println("Wednesday"); break;
case 5: System.out.println("Thursday"); break;
case 6: System.out.println("Friday"); break;
case 7: System.out.println("Saturday"); break;
default: System.out.println("Wrong number!"); break;
}
}
5
}