Package Title: Lab Questions Course Title: Big Java



Download 144.2 Kb.
Page3/4
Date29.06.2021
Size144.2 Kb.
1   2   3   4
10) Draw a flowchart for a program that reads two integers and prints the smaller number.

11.1) Write a program that prompts the user to enter three strings. Compare the String objects lexicographically and print the middle-valued string. For example, if the three strings were "abcd", "wxyz", and "pqrs", the program would print "pqrs". Limit yourself to simple, nested if statements that don’t use the Boolean operators && or ||. Be sure and test your code by giving it input data that tests every path through your code. Make a list of values for str1, str2, and str3 that would thoroughly test the code.

11.2) Rewrite the program solution for Lab 5.11.1 using the Boolean operator && to simplify the logical structure.

12) Programmers take many visual cues from the indenting in a program, so it is imperative that the indentation we provide reflects the logic of the program.

Consider the program below, which is extremely difficult to read because it is so badly indented. Take the program and indent it properly so that the indents reflect the logical structure of the program.

Some programmers adopt a style for if statements in which each alternative is always represented as a block of code surrounded by {}. There are a couple of advantages to this style:

the braces clearly indicate the true and false alternatives, and

the program is easier to maintain if you need to add more lines within one of the alternatives in the future.

After indenting the following code, add {} to all the alternatives.

public class BadIfs

{

public static void main(String[] args)



{

int x = 9;

int y = 3;

int z = 7;

if (x < y){System.out.println("aaa"); if (x < z) System.out.println("bbb"); } else System.out.println("ccc");System.out.println("ddd"); if (y > z)if (z > x)

System.out.println("eee");

else System.out.println("fff"); else

System.out.println("ggg"); }

}

Here is the output of the program if you run it as listed:

ccc


ddd

ggg


Make sure that the program still produces the same output when you have indented it properly.

13.1) Build and run the following program. What happens when the two points have the same x-coordinate?

import java.util.Scanner;


public class Slope

{

public static void main(String[] args)



{

Scanner in = new Scanner(System.in);


System.out.print("Input x coordinate of the first point: ");

double xcoord1 = in.nextDouble();


System.out.print("Input y coordinate of the first point: ");

double ycoord1 = in.nextDouble();


System.out.print("Input x coordinate of the second point: ");

double xcoord2 = in.nextDouble();


System.out.print("Input y coordinate of the second point: ");

double ycoord2 = in.nextDouble();


double slope = (ycoord2 - ycoord1) / (xcoord2 – xcoord1);

System.out.println("The slope of the line is " + slope);



}

}
13.2) Correct and rebuild the slope program to disallow a vertical line (denominator = 0).



14) Complete the following code to test whether two circles, each having a user-defined radius and a fixed center point lying along the same horizontal line, are disjoint, overlapping, or mutually contained.


public class CircleOverlap

{

public static void main(String[] args)



{

Scanner in = new Scanner(System.in);


System.out.print("Input the radius of the first circle: ");

double radius1 = in.nextDouble();

double xcenter1 = 0;

double ycenter1 = 0;

System.out.print("Input the radius of the second circle: ");

double radius2 = in.nextDouble();

double xcenter2 = 40;

double ycenter2 = 0;


// Your work goes here

}

}




Share with your friends:
1   2   3   4




The database is protected by copyright ©essaydocs.org 2020
send message

    Main page