Capitulo 4.
Código: 11.php
<?php
$level = $score = $time = 0;
?>
Código: 12.php
<?php
$month = "March";
if ($month == "March") echo "It's springtime";
?>
Blog sobre linux y... linux y... linux y... otras cosas de linux
Capitulo 4.
Código: 11.php
<?php
$level = $score = $time = 0;
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);}}
/*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);}}
/*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.");}}
/*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");}}
/*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!");}}
/*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);}}}
/*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}}}}
/*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.");}}
/*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);}}
// 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);}}
// 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));}}
/*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.");}}
// 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");}}
// 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);}}
// 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);}}
// 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);}}}
/*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!");}}}
// 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);}}
// 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");}}
// 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);}}
// 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 !!
}
}
// 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);}}
// *** 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);}}
// 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);}}
/*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));}}
// 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);}}
// 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();}}}
// 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);}}
// 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 **");}}
// 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.");}}
// 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");}}}
// 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");}}}
// 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");}}}
// 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();}}}
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();}}}
/*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.");}}}
Capitulo 4. Código: 11.php <?php $level = $score = $time = 0; ?> Código: 12.php <?php $month = "March"; if ($mont...