0% found this document useful (0 votes)
153 views5 pages

Java Examples I Fall 2010

The document contains 23 Java examples demonstrating basic Java programming concepts like variables, data types, operators, conditional statements, loops, methods, classes and more. The examples range from simple print statements and variable assignments to more complex examples using loops, conditionals, methods and Scanner input. Overall the examples provide an introduction to fundamental Java programming structures and syntax.

Uploaded by

Tabi
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
0% found this document useful (0 votes)
153 views5 pages

Java Examples I Fall 2010

The document contains 23 Java examples demonstrating basic Java programming concepts like variables, data types, operators, conditional statements, loops, methods, classes and more. The examples range from simple print statements and variable assignments to more complex examples using loops, conditionals, methods and Scanner input. Overall the examples provide an introduction to fundamental Java programming structures and syntax.

Uploaded by

Tabi
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 5

CSCI 120: Fall Semester 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[]){

System.out.println ("Enter an integer!");

Scanner sc = new Scanner(System.in);


int i = sc.nextInt();

System.out.println (" i = "+i);


}
}

import java.util.*;
class Ex7{
public static void main (String args[]){

System.out.println ("Enter a float number!");

Scanner sc = new Scanner(System.in);


float f = sc.nextFloat();

System.out.println (" f = "+f);


}
}

public class Ex8 {


public static void main(String[] args) {
double price, tax;
price = 500;
tax = 17.5;
price = price * (1 + tax/100); // calculate cost

System.out.println ("*** Product Price Check ***");


System.out.println ("Cost after tax = " + price);
}
}

import java.util.*;
class Ex9{
public static void main (String args[]){

System.out.println ("Enter a boolean!");

Scanner sc = new Scanner(System.in);


boolean b = sc.nextBoolean();

System.out.println (" b = "+b);


}
}

import java.util.*;
class Ex10{
public static void main (String args[]){
System.out.println ("Enter a string!");

Scanner sc = new Scanner(System.in);


String s = sc.next();
3
System.out.println (" s = "+s);
}

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);
}
}
}

class Ex16 { //nested loops


public static void main(String[] args ) {
for(int i = 1; i <= 5; i++) {
for (int j = 1; j<=5; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
4

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
}

class tenNumbers_for { //FOR LOOP


public static void main(String[] args){
for (int counter = 1; counter < 11; counter ++){
System.out.println(counter);
}
}
}

class tenNumbers_while { { //WHILE LOOP


public static void main(String[] args){
int counter = 1;
while (counter < 11) {
System.out.println(counter);
counter++;
}
}
}

class tenNumbers_do { //DO LOOP


public static void main(String[] args){
int counter = 1;
do {
System.out.println(counter);
counter ++;
} while (counter < 10);
}
}

You might also like