CSI 250L Lab Exercises, Fall 2011
American University of Science and Technology · Programming II Lab · 4 pages
More CSI 250L files
- Lab 1 Solution · Spring 2012Lab · with solutionView
- Lab 1 · Spring 2012LabView
- Lab 10 Solution · Spring 2012Lab · with solutionView
- Lab 10 · Spring 2012LabView
- Lab 11 Solution · Spring 2012Lab · with solutionView
- Lab 11 · Spring 2012LabView
- Lab 12 Solution · Spring 2012Lab · with solutionView
- Lab 12 · Spring 2012LabView
Show the text of this lab
1
American University of Science & Technology
Faculty of Arts and Sciences
Department Of Computer Science
CSI250L / ICT311L – Programming II Lab
Fall 2015/2016
Lab Work 2
Problem1:
Trace the following java applications:
a. What results from attempting to compile and run the following code?
public class Conditional{
public static void main( String args[] )
{
int x = 4;
System.out.println( "Value is"+ ( ( x > 4 ) ? 99.9 : 9 ));
}
}
A. The output: value is 99.9
B. The output: value is 9
C. The output: value is 9.0
D. A compiler error at Line 5.
2
b. What is the output of the following program? Assume the user inputs 12 for one
execution of the program and 15 for a second execution?
import javax.swing.JOptionPane;
public class Compares{
public static void main( String a[] )
{
int integer;
String input;
input = JOptionPane.showInputDialog( "Enter an integer:" );
integer = Integer.parseInt( input );
if( (integer % 6 ) == 0 )
System.out.println( "Hello");
else
System.out.println("Good Bye");
}
}
c. What would happen when the following application is compiled and executed? Select
the one correct answer.
public class Compares
{
public static void main(String args[])
{
int x = 10, y;
if(x < 10)
y = 1;
if(x>= 10) y = 2;
System.out.println("y is " + y);
}
}
A. The program compiles and prints y is 0 when executed.
B. The program compiles and prints y is 1 when executed.
C. The program compiles and prints y is 2 when executed.
D. The program does not compile complaining about y not being initialized.
E. The program throws a runtime exception.
3
d. What would happen when the following application is compiled and executed? Select
the one correct answer.
public class Compares
{
public static void main(String args[])
{
boolean flag = false;
if( flag == true )
System.out.println( "true");
else
System.out.println( "false");
}
}
A. It will print false;
B. it will print true;
C. It will print none.
D. Compilation error.
E. Runtime error.
Problem 2:
Find the errors in the following application and correct them:
import java.util.scanner;
public class check
{
public static void main(string []args)
{
int x, y;
Scanner s=new Scanner(System.in);
system.out.println("enter an integer:");
x=s.nextint();
y= x*2;
System.out.printf("x= %d, y= %f",x,y);
}
}
4
Problem 3:
Type the following java application then apply the following listed steps:
import java.util.Scanner;
public class Example1
{
public static void main(String args[])
{
double radius;
Scanner sc = new Scanner(System.in);
System.out.println("Enter The Radius");
radius = sc.nextDouble();
System.out.println("The area is "+ 2*3.14*(radius*radius));
}
}
1. Compile then execute the program from the cmd prompt.
2. Modify the program to compute the circle
and use printf statement to output
the circumference value.
3. Modify the program to provide the input and output using JOptionPane methods.
Problem 4:
Write a java application that input an integer X, >0 (use a Scanner object) from the user
and displays:
The factorial all even integers from 1 to X.
The cubic value of all integers from 1 to X
All prime numbers from 1to X.
Problem 5:
Write an application that prompts the user to enter a 5-digit integer (Use a Scanner
object). Separate the number into its individual digits, and then display the following:
(Use printf statement)
a- The sum of the five digits being extracted.
b- The number of digits that are even.
c- The number of digits that are odd.
d- The number of digits that are zero.
Example: For the input 10234, the output will be:
Sum of digits: 1 + 0 + 2 + 3 + 4 = 10.
Zero digits count: 1.
Odd digits count: 2.
Even digits count: 2.
(Modify the code so it works for a number of N digits.)