Problemas Resueltos Sobre Programacion (Cap5 Libro An Introduction To Object-Oriented Programming With Java)
* Indent the following if statements properly.
a. if (a==b) if (c==d) a =1; else b=1; else c=1;
No puede haber un else dentro de otro else.
b. if (a==b) a =1; if (c==d) b=1; else c=1;
c. if (a==b) {if (c==d) a =1; b=2 ;} else {b=1};
d. if (a==b){
if (c==d) a =1; b=2;}
else {b=1; if (a==d) d=3;}
Pregunta 2
* Which two of the following threeif statements are equivalent?
a. if(a == b)
if (c == d) a = 1;
else b = 1;
b. if(a == b) {
if (c == d) a = 1; }
else b = 1;
c. if(a == b)
if (c == d) a = 1;
else b = 1;
- a y b son equivalentes
Pregunta 3
* Evaluate the following boolean expressions. For each of thefollowing expressions, assume x is 10, y is 20, and z is 30. Indicate which of the following boolean expressions are always true and which are always false, regardless of the values for x, y, or z.
a. V || F
x< 10 | | x > 10 (F)
b. V && F F
x> y && y > x (F) Siempre va a ser falso
c. V && V
(x< y + z) && (x +10 <= 20) (V)
d. V && V
z - y == x && Math.abs(y - z) == x (V)
e. F && F
x< 10 && x > 10 (F) Siempre va a ser falso
f. F || V
x> y || y > x (V) Siempre va a ser verdadero
g. F || F
!(x< y + z) || !(x + 10 <= 20) (F)
h. !(x == y)) && (x != y) &&(x< y || y < x) (V)
V && V && V
V
Pregunta 4
import java.util.Scanner;
class Practica {
public static void main (String[] args ) {
int clase;
Scanner scanner;
scanner = new Scanner(System.in);
System.out.print("Ingrese Numero de clase: ");
clase = scanner.nextInt();
if (clase > 7){if (clase >8)
System.out.print("a=1 b=2");
if (clase ==8)
System.out.print("a=3 b=4");
}
else {
System.out.print("a=5");
}
}
}
Pregunta 5
import java.util.Scanner;
class Menor{
public static void main( String[] args ) {
int a, b , c;
Scanner scanner;
scanner = newScanner(System.in);
System.out.print("Ingrese Numero 1: ");
a = scanner.nextInt();
System.out.print("Ingrese Numero 2: ");
b = scanner.nextInt();
System.out.print("Ingrese Numero 3: ");
c = scanner.nextInt();
System.out.print("El menor es ");
if (a<b) {
if (a<c) {
System.out.print (a);
}
else {
System.out.print (c);}
}
else {
if (b<c) {
System.out.print(b);
}
else {
System.out.print(c);
}
}
}
}
Pregunta 6
a = 0 ;
false
true
choice==1?
b = 1;
c = 2;
choice= =2?
choice==3?
false
false
true
true
false
default?
d = 3;
true
a = 0;
false
true
Choice==1?
b = 1;
c = 2;
Choice==2?
Choice==3?
false
falsetrue
true
break;
break;
break;
Default?
c = 2;
break;
false
true
d = 3;
Pregunta 7
import java.util.Scanner;
class Youwon {
public static void main (String[] args ) {
int puntos;
Scanner scanner;
scanner = new Scanner(System.in);
System.out.print("Ingrese el total de puntos: ");
puntos = scanner.nextInt();
if(puntos >= 500)
System.out.print("You won a free cup of coffee and a regular-size doughnut and a 12-oz orange juice and a combo breakfast and a reserved table for one week.");
else if (puntos >= 400)
System.out.print("You won a free cup of coffee and a regular-size doughnut and a 12-oz orange juice and a combo breakfast.");
else if (puntos >= 300)
System.out.print("You...
Regístrate para leer el documento completo.