You are on page 1of 3

King Saud University

College of Computer and Information Sciences


Information Technology Department

First Semester 1431/1432


CSC 111: Introduction to Programming with Java
TUTORIAL # 2
Chap3: Introduction to Object and Input/Output

Q1. Trace the following program:


import java.util.Scanner;
public class TestProg
{
public static void main( String args[] )
{ int n1;
int n2;
String s1 = "csc111 Java programming", s2, s3;
n1 = (int) (s1.charAt (5)) ;
n2 = indexOf('g',20);
s2 = s1.substring (7, 11);
s3 = s2.concat (s1.substring (0, 5) + (char) n1);
System.out.printf("s1 = %25s%ns2 = %10s%ns3 = %10s%n" , s1, s3, s2);
System.out.printf("n1 = %d%n" , n1++);

}
}

Q2. Given a string s ,write statements to do the following:


a. Print the first, last and middle characters of s.
b. Print the character before the last character.
c. Print the string that is formed by deleting the first and last characters of s.
d. Print the string that is formed when the string "mid" is inserted in the middle of s.
e. Print the middle 5 characters of s.

Q3. Given the following declarations :


char c1 = 'a'; char c2 = 'x'; String s1 = "Saudi-Arabia"; String s2 = "arab";
int n = 5;

What is the output of the following?


a. System.out.print(s1.charAt(n)+"\n");
b. System.out.print(s1.replace( c1 , c2 ));
c. System.out.print(s2.concat("ic" + s1));
d. System.out.print(s1.startsWith(s2);
e. System.out.print(s1.compareTo("saudi"));
f. System.out.print(s2.compareTo("ar"+"ab")));
g. System.out.print(s1.equals("Saudi Arabia"));
h. System.out.print(s2.indexOf(c1) );
i. System.out.print(s1.indexOf('a' , 5) );

1
j. System.out.print(s1.indexOf(s2) );
k. System.out.print(s1.regionMatches(7,s2,1,2 );

l. System.out.printf("%5d",s1.indexOf(s2.substring(1,3)) );
m. System.out.print(s1.indexOf(s2.substring(1,3) , 8) );
n. System.out.printf("%.2f",(double)s1.length() – s2.length());
o. System.out.printf("%15s",s1.substring( 6 , 12) . toLowerCase());

Q4. Write a statement for each of the following:


a. Print 1234 right-justified in a 10 digit field.
b. Print 123.456789 in exponential notation and 3 digits of precision.
c. Print 3.333333 with a sign (+ or -) in a field of 20 characters with a precision of 3.

Q5. Trace the following program, using the following input.


Name : Deitel
Sales : 500000
import javax.swing.JOptionPane;

public class Home1


{
public static void main(String[] args)
{
final double RATE = .06;
String owner, outputStr1, outputStr2, outputStr3, title;
double price, commission, cost;

owner = JOptionPane.showInputDialog("This program asks the "


+ "user for a name and selling\n amount of a home, "
+ "and then calculates the cost to sell the home\n and the "
+ "commission of an individual sales agent.\n\n"
+ "Please enter owner's last name: ");

price = Double.parseDouble(
JOptionPane.showInputDialog("Please enter the sales "
+ "price of the home."));

cost = RATE * price;


commission = cost/4.0;
title = (owner + "\'s Home Sale");

outputStr1 =
String.format("The %s's home sold for $%.2f", owner, price);
outputStr2 =
String.format("%nThe cost to sell the home was $%.2f", cost);
outputStr3 =
String.format("%nThe selling or listing agent earned $%.2f",
commission);

JOptionPane.showMessageDialog(null, outputStr1 + outputStr2


+ outputStr3, title, JOptionPane.INFORMATION_MESSAGE);

System.exit(0);
}

2
}

You might also like