Códigos del capitulo 1.
Archivo: Example.java
/*
This is a simple Java program.
Call this file Example.java.
*/
class Example {
// A Java program begins with a call to main().
public static void main(String[] args) {
System.out.println("Java drives the Web.");
}}
Archivo: Example2.java
/*This demonstrates a variable.Call this file Example2.java.*/class Example2 {public static void main(String[] args) {int myVar1; // this declares a variableint myVar2; // this declares another variablemyVar1 = 1024; // this assigns 1024 to myVar1System.out.println("myVar1 contains " + myVar1);myVar2 = myVar1 / 2;System.out.print("myVar2 contains myVar1 / 2: ");System.out.println(myVar2);}}
Archivo: Example3.java
/*This program illustrates the differencesbetween int and double.Call this file Example3.java.*/class Example3 {public static void main(String[] args) {int v; // this declares an int variabledouble x; // this declares a floating-point variablev = 10; // assign v the value 10x = 10.0; // assign x the value 10.0System.out.println("Original value of v: " + v);System.out.println("Original value of x: " + x);System.out.println(); // print a blank line// now, divide both by 4v = v / 4;x = x / 4;System.out.println("v after division: " + v);System.out.println("x after division: " + x);}}
Archivo: GalToLit.java
/*Try This 1-1This program converts gallons to liters.Call this program GalToLit.java.*/class GalToLit {public static void main(String[] args) {double gallons; // holds the number of gallonsdouble liters; // holds conversion to litersgallons = 10; // start with 10 gallonsliters = gallons * 3.7854; // convert to litersSystem.out.println(gallons + " gallons is " + liters + " liters.");}}
Archivo: IfDemo.java
/*Demonstrate the if.Call this file IfDemo.java.*/class IfDemo {public static void main(String[] args) {int a, b, c;a = 2;b = 3;if(a < b) System.out.println("a is less than b");// this won't display anythingif(a == b) System.out.println("you won't see this");System.out.println();c = a - b; // c contains -1System.out.println("c contains -1");if(c >= 0) System.out.println("c is non-negative");if(c < 0) System.out.println("c is negative");System.out.println();c = b - a; // c now contains 1System.out.println("c contains 1");if(c >= 0) System.out.println("c is non-negative");if(c < 0) System.out.println("c is negative");}}
Archivo: ForDemo.java
/*Demonstrate the for loop.Call this file ForDemo.java.*/class ForDemo {public static void main(String[] args) {int count;for(count = 0; count < 5; count = count+1)System.out.println("This is count: " + count);System.out.println("Done!");}}
Archivo: BlockDemo.java
/*Demonstrate a block of code.Call this file BlockDemo.java.*/class BlockDemo {public static void main(String[] args) {double i, j, d;i = 5;j = 10;// the target of this if is a blockif(i != 0) {System.out.println("i does not equal zero");d = j / i;System.out.println("j / i is " + d);}}}
Archivo: GalToLitTable.java
/*Try This 1-2This program displays a conversiontable of gallons to liters.Call this program "GalToLitTable.java".*/class GalToLitTable {public static void main(String[] args) {double gallons, liters;int counter;counter = 0;for(gallons = 1; gallons <= 100; gallons++) {liters = gallons * 3.7854; // convert to litersSystem.out.println(gallons + " gallons is " +liters + " liters.");counter++;// every 10th line, print a blank lineif(counter == 10) {System.out.println();counter = 0; // reset the line counter}}}}
Códigos del capitulo 2.
Archivo: Inches.java
/*Compute the number of cubic inchesin 1 cubic mile.*/class Inches {public static void main(String[] args) {long ci;long im;im = 5280 * 12;ci = im * im * im;System.out.println("There are " + ci +" cubic inches in cubic mile.");}}
Archivo: Hypot.java
/*Use the Pythagorean theorem to findfind the length of the hypotenusegiven the lengths of the two opposingsides.*/class Hypot {public static void main(String[] args) {double x, y, z;x = 3;y = 4;z = Math.sqrt(x*x + y*y);System.out.println("Hypotenuse is " +z);}}
Archivo: CharArithDemo.java
// Character variables can be handled like integers.class CharArithDemo {public static void main(String[] args) {char ch;ch = 'X';System.out.println("ch contains " + ch);ch++; // increment chSystem.out.println("ch is now " + ch);ch = 90; // give ch the value ZSystem.out.println("ch is now " + ch);}}
Archivo: BoolDemo.java
// Demonstrate boolean values.class BoolDemo {public static void main(String[] args) {boolean b;b = false;System.out.println("b is " + b);b = true;System.out.println("b is " + b);// a boolean value can control the if statementif(b) System.out.println("This is executed.");b = false;if(b) System.out.println("This is not executed.");// outcome of a relational operator is a boolean valueSystem.out.println("10 > 9 is " + (10 > 9));}}
Archivo: Sound.java
/*Try This 2-1Compute the distance to a lighteningstrke whose sound takes 7.2 secondsto reach you.*/class Sound {public static void main(String[] args) {double dist;dist = 7.2 * 1100;System.out.println("The lightening is " + dist +" feet away.");}}
Archivo: StrDemo.java
// Demonstrate escape sequences in strings.class StrDemo {public static void main(String[] args) {System.out.println("First line\nSecond line");System.out.println("A\tB\tC");System.out.println("D\tE\tF");}}
Archivo: DynInit.java
// Demonstrate dynamic initialization.class DynInit {public static void main(String[] args) {double radius = 4, height = 5;// dynamically initializ volumedouble volume = 3.1416 * radius * radius * height;System.out.println("Volume is " + volume);}}
Archivo: ScopeDemo.java
// Demonstrate block scope.class ScopeDemo {public static void main(String[] args) {int x; // known to all code within mainx = 10;if(x == 10) { // start new scopeint y = 20; // known only to this block// x and y both known here.System.out.println("x and y: " + x + " " + y);x = y * 2;}// y = 100; // Error! y not known here// x is still known here.System.out.println("x is " + x);}}
Archivo: VarInitDemo.java
// Demonstrate lifetime of a variable.class VarInitDemo {public static void main(String[] args) {int x;for(x = 0; x < 3; x++) {int y = -1; // y is initialized each time block is enteredSystem.out.println("y is: " + y); // this always prints -1y = 100;System.out.println("y is now: " + y);}}}
Archivo: NestVar.java
/*This program attempts to declared a variablein an inner scope with the same name as onedefined in an outer scope.*** This program will not compile. ****/class NestVar {public static void main(String[] args) {int count;for(count = 0; count < 10; count = count+1) {System.out.println("This is count: " + count);int count; // illegal!!!for(count = 0; count < 2; count++)System.out.println("This program is in error!");}}}
Archivo: ModDemo.java
// Demonstrate the % operator.class ModDemo {public static void main(String[] args) {int iresult, irem;double dresult, drem;iresult = 10 / 3;irem = 10 % 3;dresult = 10.0 / 3.0;drem = 10.0 % 3.0;System.out.println("Result and remainder of 10 / 3: " +iresult + " " + irem);System.out.println("Result and remainder of 10.0 / 3.0: " +dresult + " " + drem);}}
Archivo: RelLogOps.java
// Demonstrate the relational and logical operators.class RelLogOps {public static void main(String[] args) {int i, j;boolean b1, b2;i = 10;j = 11;if(i < j) System.out.println("i < j");if(i <= j) System.out.println("i <= j");if(i != j) System.out.println("i != j");if(i == j) System.out.println("this won't execute");if(i >= j) System.out.println("this won't execute");if(i > j) System.out.println("this won't execute");b1 = true;b2 = false;if(b1 & b2) System.out.println("this won't execute");if(!(b1 & b2)) System.out.println("!(b1 & b2) is true");if(b1 | b2) System.out.println("b1 | b2 is true");if(b1 ^ b2) System.out.println("b1 ^ b2 is true");}}
Archivo: SCops.java
// Demonstrate the short-circuit operators.class SCops {public static void main(String[] args) {int n, d, q;n = 10;d = 2;if(d != 0 && (n % d) == 0)System.out.println(d + " is a factor of " + n);d = 0; // now, set d to zero// Since d is zero, the second operand is not evaluated.if(d != 0 && (n % d) == 0)System.out.println(d + " is a factor of " + n);/* Now, try same thing without short-circuit operator.This will cause a divide-by-zero error.*/if(d != 0 & (n % d) == 0)System.out.println(d + " is a factor of " + n);}}
Archivo: SideEffects.java
// Side-effects can be important.
class SideEffects {
public static void main(String[] args) {
int i;
i = 0;
/* Here, i is still incremented even though
the if statement fails. */
if(false & (++i < 100))
System.out.println("this won't be displayed");
System.out.println("if statements executed: " + i); // displays 1
/* In this case, i is not incremented because
the short-circuit operator skips the increment. */
if(false && (++i < 100))
System.out.println("this won't be displayed");
System.out.println("if statements executed: " + i); // still 1 !!
}
}
Archivo: LtoD.java
// Demonstate automatic conversion from long to double.class LtoD {public static void main(String[] args) {long L;double D;L = 100123285L;D = L;System.out.println("L and D: " + L + " " + D);}}
Archivo: DtoL.java
// *** This program will not compile. ***class DtoL {public static void main(String[] args) {long L;double D;D = 100123285.0;L = D; // Illegal!!!System.out.println("L and D: " + L + " " + D);}}
Produce un error en la compilación
Archivo: CastDemo.java
// Demonstrate casting.class CastDemo {public static void main(String[] args) {double x, y;byte b;int i;char ch;x = 10.0;y = 3.0;i = (int) (x / y); // cast double to intSystem.out.println("Integer outcome of x / y: " + i);i = 100;b = (byte) i;System.out.println("Value of b: " + b);i = 257;b = (byte) i;System.out.println("Value of b: " + b);b = 88; // ASCII code for Xch = (char) b;System.out.println("ch: " + ch);}}
Archivo: LogicalOpTable.java
/*Try This 2-2Print a truth table for the logical operators.*/class LogicalOpTable {public static void main(String[] args) {boolean p, q;System.out.println("P\tQ\tAND\tOR\tXOR\tNOT");p = true; q = true;System.out.print(p + "\t" + q +"\t");System.out.print((p&q) + "\t" + (p|q) + "\t");System.out.println((p^q) + "\t" + (!p));p = true; q = false;System.out.print(p + "\t" + q +"\t");System.out.print((p&q) + "\t" + (p|q) + "\t");System.out.println((p^q) + "\t" + (!p));p = false; q = true;System.out.print(p + "\t" + q +"\t");System.out.print((p&q) + "\t" + (p|q) + "\t");System.out.println((p^q) + "\t" + (!p));p = false; q = false;System.out.print(p + "\t" + q +"\t");System.out.print((p&q) + "\t" + (p|q) + "\t");System.out.println((p^q) + "\t" + (!p));}}
Archivo: PromDemo.java
// A promotion surprise!class PromDemo {public static void main(String[] args) {byte b;int i;b = 10;i = b * b; // OK, no cast neededb = 10;b = (byte) (b * b); // cast needed!!System.out.println("i and b: " + i + " " + b);}}
Archivo: UseCast.java
// Using a cast.class UseCast {public static void main(String[] args) {int i;for(i = 0; i < 5; i++) {System.out.println(i + " / 3: " + i / 3);System.out.println(i + " / 3 with fractions: "+ (double) i / 3);System.out.println();}}}
Códigos del capitulo 3.
Archivo: KbIn.java
// Read a character from the keyboard.
class KbIn {public static void main(String[] args)throws java.io.IOException {char ch;System.out.print("Press a key followed by ENTER: ");ch = (char) System.in.read(); // get a charSystem.out.println("Your key is: " + ch);}}
Archivo: Guess.java
// Guess the letter game.class Guess {public static void main(String[] args)throws java.io.IOException {char ch, answer = 'K';System.out.println("I'm thinking of a letter between A and Z.");System.out.print("Can you guess it: ");ch = (char) System.in.read(); // read a char from the keyboardif(ch == answer) System.out.println("** Right **");}}
Archivo: Guess2.java
// Guess the letter game, 2nd version.class Guess2 {public static void main(String[] args)throws java.io.IOException {char ch, answer = 'K';System.out.println("I'm thinking of a letter between A and Z.");System.out.print("Can you guess it: ");ch = (char) System.in.read(); // get a charif(ch == answer) System.out.println("** Right **");else System.out.println("...Sorry, you're wrong.");}}
Archivo: Guess3.java
// Guess the letter game, 3rd version.class Guess3 {public static void main(String[] args)throws java.io.IOException {char ch, answer = 'K';System.out.println("I'm thinking of a letter between A and Z.");System.out.print("Can you guess it: ");ch = (char) System.in.read(); // get a charif(ch == answer) System.out.println("** Right **");else {System.out.print("...Sorry, you're ");// a nested ifif(ch < answer) System.out.println("too low");else System.out.println("too high");}}}
Archivo: Ladder.java
// Demonstrate an if-else-if ladder.class Ladder {public static void main(String[] args) {int x;for(x=0; x<6; x++) {if(x==1)System.out.println("x is one");else if(x==2)System.out.println("x is two");else if(x==3)System.out.println("x is three");else if(x==4)System.out.println("x is four");elseSystem.out.println("x is not between 1 and 4");}}}
Archivo: SwitchDemo.java
// Demonstrate the switch.class SwitchDemo {public static void main(String[] args) {int i;for(i=0; i<10; i++)switch(i) {case 0:System.out.println("i is zero");break;case 1:System.out.println("i is one");break;case 2:System.out.println("i is two");break;case 3:System.out.println("i is three");break;case 4:System.out.println("i is four");break;default:System.out.println("i is five or more");}}}
Archivo: NoBreak.java
// Demonstrate the switch without break statements.class NoBreak {public static void main(String[] args) {int i;for(i=0; i<=5; i++) {switch(i) {case 0:System.out.println("i is less than one");case 1:System.out.println("i is less than two");case 2:System.out.println("i is less than three");case 3:System.out.println("i is less than four");case 4:System.out.println("i is less than five");}System.out.println();}}}
Archivo: NoBreak1.java
class NoBreak1 {public static void main(String[] args) {int i;for(i=0; i<=5; i++) {switch(i) {case 1:case 2:case 3: System.out.println("i is 1, 2 or 3");break;case 4: System.out.println("i is 4");break;}System.out.println();}}}
Archivo: Help.java
/*Try This 3-1A simple help system.*/class Help {public static void main(String[] args)throws java.io.IOException {char choice;System.out.println("Help on:");System.out.println(" 1. if");System.out.println(" 2. switch");System.out.print("Choose one: ");choice = (char) System.in.read();System.out.println("\n");switch(choice) {case '1':System.out.println("The if:\n");System.out.println("if(condition) statement;");System.out.println("else statement;");break;case '2':System.out.println("The traditional switch:\n");System.out.println("switch(expression) {");System.out.println(" case constant:");System.out.println(" statement sequence");System.out.println(" break;");System.out.println(" // ...");System.out.println("}");break;default:System.out.print("Selection not found.");}}}
Archivo: SqrRoot.java
// Show square roots of 1 to 99 and the rounding error.
class SqrRoot {
public static void main(String[] args) {
double num, sroot, rerr;
for(num = 1.0; num < 100.0; num++) {
sroot = Math.sqrt(num);
System.out.println("Square root of " + num +
" is " + sroot);
// compute rounding error
rerr = num - (sroot * sroot);
System.out.println("Rounding error is " + rerr);
System.out.println();
}
}
}
Archivo: DecrFor.java
// A negatively running for loop.
class DecrFor {
public static void main(String[] args) {
int x;
for(x = 100; x > -100; x -= 5)
System.out.println(x);
}
}
Archivo: Comma.java
// Use commas in a for statememt.
class Comma {
public static void main(String[] args) {
int i, j;
for(i=0, j=10; i < j; i++, j--)
System.out.println("i and j: " + i + " " + j);
}
}
Archivo: ForTest.java
// Loop until an S is typed.
class ForTest {
public static void main(String[] args)
throws java.io.IOException {
int i;
System.out.println("Press S to stop.");
for(i = 0; (char) System.in.read() != 'S'; i++)
System.out.println("Pass #" + i);
}
}
Archivo: Empty.java
// Parts of the for can be empty.
class Empty {
public static void main(String[] args) {
int i;
for(i = 0; i < 10; ) {
System.out.println("Pass #" + i);
i++; // increment loop control var
}
}
}
Archivo: Empty2.java
// Move more out of the for loop.
class Empty2 {
public static void main(String[] args) {
int i;
i = 0; // move initialization out of loop
for(; i < 10; ) {
System.out.println("Pass #" + i);
i++; // increment loop control var
}
}
}
Archivo: Empty3.java
// The body of a loop can be empty.
class Empty3 {
public static void main(String[] args) {
int i;
int sum = 0;
// sum the numbers through 5
for(i = 1; i <= 5; sum += i++) ;
System.out.println("Sum is " + sum);
}
}
Archivo: ForVar.java
// Declare loop control variable inside the for.
class ForVar {
public static void main(String[] args) {
int sum = 0;
int fact = 1;
// compute the factorial of the numbers through 5
for(int i = 1; i <= 5; i++) {
sum += i; // i is known throughout the loop
fact *= i;
}
// but, i is not known here.
System.out.println("Sum is " + sum);
System.out.println("Factorial is " + fact);
}
}
Archivo: WhileDemo.java
// Demonstrate the while loop.
class WhileDemo {
public static void main(String[] args) {
char ch;
// print the alphabet using a while loop
ch = 'a';
while(ch <= 'z') {
System.out.print(ch);
ch++;
}
}
}
Archivo: Power.java
// Compute integer powers of 2.
class Power {
public static void main(String[] args) {
int e;
int result;
for(int i=0; i < 10; i++) {
result = 1;
e = i;
while(e > 0) {
result *= 2;
e--;
}
System.out.println("2 to the " + i + " power is " + result);
}
}
}
08/01/23
Archivo: DWDemo.java
// Demonstrate the do-while loop.
class DWDemo {
public static void main(String[] args)
throws java.io.IOException {
char ch;
do {
System.out.print("Press a key following by ENTER: ");
ch = (char) System.in.read(); // get a char
} while(ch != 'q');
}
}
Archivo: Guess4.java
// Guess the letter game, 4th version.
class Guess4 {
public static void main(String[] args)
throws java.io.IOException {
char ch, ignore, answer = 'K';
do {
System.out.println("I'm thinking of a letter between A and Z.");
System.out.print("Can you guess it: ");
// read a character
ch = (char) System.in.read();
// discard any other characters in the input buffer
do {
ignore = (char) System.in.read();
} while(ignore != '\n');
if(ch == answer) System.out.println("** Right **");
else {
System.out.print("...Sorry, you're ");
if(ch < answer) System.out.println("too low");
else System.out.println("too high");
System.out.println("Try again!\n");
}
} while(answer != ch);
}
}
09/01/23
No hay comentarios:
Publicar un comentario