0% found this document useful (0 votes)
217 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)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
217 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)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

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;
[Link] (" 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)
[Link] ("a > b");
else
[Link] (" 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;
[Link] ("c = " +c);
}
}

class Ex3 {
public static void main (String args[]){
String s ="CSCI 120";
[Link] ("s = " +s);
}
}

class Ex4 {
public static void main (String args[]){
float f=3.14f;
[Link] ("f = " +f);
}
}

class Ex5 {
public static void main (String args[]){
char c= 'N';
[Link] ("c = " +c);
2
}
}

import [Link].*;
class Ex6{
public static void main (String args[]){

[Link] ("Enter an integer!");

Scanner sc = new Scanner([Link]);


int i = [Link]();

[Link] (" i = "+i);


}
}

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

[Link] ("Enter a float number!");

Scanner sc = new Scanner([Link]);


float f = [Link]();

[Link] (" 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

[Link] ("*** Product Price Check ***");


[Link] ("Cost after tax = " + price);
}
}

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

[Link] ("Enter a boolean!");

Scanner sc = new Scanner([Link]);


boolean b = [Link]();

[Link] (" b = "+b);


}
}

import [Link].*;
class Ex10{
public static void main (String args[]){
[Link] ("Enter a string!");

Scanner sc = new Scanner([Link]);


String s = [Link]();
3
[Link] (" s = "+s);
}

import [Link].*;
public class Ex11 { // Scanner class
public static void main(String[] args ) {
double price, tax;
Scanner sc = new Scanner([Link]);
[Link] ("*** Product Price Check ***");
[Link] ("Enter initial price: ");
price = [Link]();
[Link]("Enter tax rate: ");
tax = [Link]();
if (price > 100) {
[Link] ("Special Promotion: Your tax will be halved! ");
tax = tax * 0.5;
}
price = price * (1 + tax/100);
[Link]("Cost after tax = " + price);
}
}
public class Ex12 { // For loop
public static void main(String[] args ) {
for (int i = 1; i <= 5; i++) {
[Link]("*****");
}
}
}
public class Ex13 { // For loop
public static void main(String[] args ) {
for (int i = 1; i <= 5; i++) {
[Link]("i=" + i);
}
}
}
public class Ex14 { // For loop
public static void main(String[] args ) {
int k=0;
for (int i = 1; i <= 5; i++) {
[Link]("i=" + i + " " + " & ");
k=k+i;
[Link]("k=" + k);
}
}
}
public class E15 { //count down
public static void main(String[] args) {
for (int i=10; i >= 1; i--) {
[Link](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++) {
[Link]("*");
}
[Link]();
}
}
}
4

import [Link].*;
class Ex17 {
public static void main(String[] args) {
int mark;
Scanner sc = new Scanner([Link]);
[Link] ("What exam mark did you get?");
mark = [Link]();
while (mark < 0 || mark > 100) { //while loop
[Link] ("invalid mark: Re-enter!");
mark = [Link]();
}
if ( mark>=96 && mark <=100)
[Link] ("A grade");
else if ( mark>=91 && mark <=95)
[Link] ("A- grade");
else if ( mark>=87 && mark <=90)
[Link] ("B+grade");
else if ( mark>=83 && mark <=86)
[Link] ("B grade");
else if ( mark>=79 && mark <=82)
[Link] ("B- grade");
else if ( mark>=75 && mark <=78)
[Link] ("C+ grade");
else if ( mark<75)
[Link] ("Not a good CSCI 120 grade");
}
}
import [Link].*;
public class Ex18 {
public static void main(String[] args ) {
int i=10;
do {
i=i-1;
[Link] ("i= " +i);
} while (i>0);
}
}
import [Link].*;
public class Ex19 {
public static void main(String[] args ) {
int i=10;
do {
i=i-1;
[Link] ("i= " +i);
} while (i>0);
}
}
import [Link].*;
class Ex20 {
public static void main(String[] args) {
Scanner sc =new Scanner ([Link]);
int day = [Link]();
switch (day) {
case 1: [Link]("Sunday"); break;
case 2: [Link]("Monday"); break;
case 3: [Link]("Tuesday"); break;
case 4: [Link]("Wednesday"); break;
case 5: [Link]("Thursday"); break;
case 6: [Link]("Friday"); break;
case 7: [Link]("Saturday"); break;
default: [Link]("Wrong number!"); break;
}
}
5
}

class tenNumbers_for { //FOR LOOP


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

class tenNumbers_while { { //WHILE LOOP


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

class tenNumbers_do { //DO LOOP


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

You might also like