I.
Give the
values of the following expressions, or state if it’s illegal.
String
a, b, c, d;
a =
"Cloud Computing!";
b =
" Algorithms "; (there
is one space before and one space after the word)
c =
"04 Mars 2013";
d =
"a";
1
|
_________9___________
|
c.indexOf("0",2)
|
2
|
_________true___________
|
a.startsWith("Cloud
C")
|
3
|
________15____________
|
a.substring(c.lastIndexOf('r'))
|
4
|
_______false_____________
|
a.substring(6,9).equals("com")
|
5
|
Colud Computing!12Cloud Computing
|
c+b.length() + a
|
6
|
_________true___________
|
d.equalsIgonreCase(“A”)
|
II.
Find the output of the java code below? What
do you think the programmer intended the code to do, and how would you fix it?
int sum = 0;
int product = 1;
int max = 20;
for (int i = 1; i
<= max; i++)
sum = sum + i;
product = product *
i;
System.out.println(“The
sum is “ + sum +“ and the product is “ + product);
The
sum is 210 and the product is 2432902008176640000. There are couple of issues
in given code. The have modified the same and highlighted below.
int
sum = 0;
long product
= 1;
int
max = 20;
for
(int i=1; i<=max; i++) {
sum
= sum + i;
product
= product * i;
}
System.out.println("The sum
is "+sum+" and the product is "+product);
product
variable you given as int. When we product from 1 to 20 then result will be
more that cannot be accommodated. so, we should change the data type from int
to long. Also, for loop curly braces were missing because of that we are
getting compilation error
III.
Write a Java GUI application that computes
the cost of flowers sold at a flower stand. Five kinds of flowers—petunia,
pansy, rose, violet, and carnation— are stocked and cost, respectively, 50¢,
75¢, $1.50, 50¢, and 80¢ per flower. Create an array of strings that holds the
names of these flowers. Create another array that holds the cost of each
corresponding flower. Your program should read the name of a flower and the
quantity desired by a customer. Locate the flower in the name array and use
that index to find the cost per stem in the cost array. Compute and print the
total cost of the sale.
/**
* @(#)midterm.java
*
* midterm application
*
* @author
* @version 1.00 2019/6/26
*/
import java.util.Scanner;
public class midterm {
public static void main(String[] args)
{
String[] flower = new String[5];
flower[0]="petunia";
flower[1]="pansy"; flower[2]="rose";
flower[3]="violet"; flower[4]="carnation";
double[] cost = new double[5];
cost[0]=.50; cost[1]=.75; cost[2]=1.50;
cost[3]=.50; cost[4]=.80;
Scanner input=new Scanner(System.in);
System.out.println("\nEnter the
name of the flower you'd like: ");
String fl=input.nextLine();
boolean correct=false;
int index=0;
for(int i=0; i<5; i++)
{
if(fl.equals(flower[i]))
{
index=i;
correct=true;
}
}
if(correct)
{
System.out.println("\nEnter
quantity desired: ");
double quantity=input.nextDouble();
double total= quantity*cost[index];
System.out.println("\nTotal
cost of sale is: $"+ total );
}
else
System.out.println("\nFlower
not found.");
}
}
0 comments:
Post a Comment