Programming and Problem Solving with Java

(やまだぃちぅ) #1
149

8.Complete the following Java application. The application should find and output
the perimeter and area of a rectangle, given the length and the width. Be sure to
label the output. Don’t forget to use comments.
//***********************************************
// Rectangle application
// This application finds the perimeter and the area
// of a rectangle, given the length and width
//***********************************************
publicclass Rectangle
{
public static voidmain(String[] args)
{
doublelength; // Length of the rectangle
doublewidth; // Width of the rectangle
doubleperimeter; // Perimeter of the rectangle
doublearea; // Area of the rectangle

length = 10.7;
width = 5.2;
9.Write an expression whose result is the position of the first occurrence of the
characters“res”in aStringvariable namedsentence. If the variable contains the
first sentence of this question, then what is the result? (Look at the sentence
carefully!)

10.Write a sequence of Java statements to output the positions of the second and
third occurrences of the characters “res”in the Stringvariable named sentence.
You may assume that there are always at least three occurrences in the variable.
(Hint:Use the substringmethod to create a new string whose contents are the
portion of sentencefollowing an occurrence of “res”.)


11.Reformat the following application to make it clear and readable.


//**************************************************************
// This application computes the sum and product of two integers
//**************************************************************
public classSumProd { public static voidmain(String[] args){
final intINT2= 8 ; final intINT1= 20 ; System.out.println(
“The sum of “+ INT1 + “ and ”
+ INT2 + “ is “+ (INT1+INT2)); System.out.println (
“Their product is “+ (INT1*INT2)); }}
Free download pdf