Study Guides (400,000)
US (230,000)
Rutgers (3,000)
4:18 (70)
Midterm

01:198:111 Study Guide - Midterm Guide: Debugging, Hypotenuse


Department
Computer Science
Course Code
01:198:111
Professor
GUNAWARDENA
Study Guide
Midterm

This preview shows half of the first page. to view the full 3 pages of the document.
CS111 Midterm 1 Review
Program Debugging
Buggy Program: https://gist.github.com/Joeb3219/66d8adc88758b58b934c24d5d97de606
Solution: https://gist.github.com/Joeb3219/535d988dcc8172ad0aba9aca7ba5f91b
Debug the following program (it should be able to run and function as expected). Look for type
errors, syntax errors, and logical errors. The class must be in a file named FaultyProgram.java:
public class faultyProgram{
public static Void main(String args)
int pi = 3.14;
System.out.println("This is a test program!");
System.out.println("Menu:")
System.out.println("1: Compute volume of sphere");
System.out.println("2: Compute area of circle");
Systemout.println("3: Compute hypotenuse of triangle");
System.println("Please enter an integer: ");
double option = IO.readInt();
if(option = 1){
System.out.println("Please enter a radius (double): ");
double radius = IO.readDouble()
double volume = (radius * radius * radius) * (4/3) * pi;
System.out.println("Volume of sphere with radius " + radius + " is: " + volume);
}else if(option == 2){
System.out.println("Please enter a radius (double): ");
double radius = IO.readString();
double area = (radius * radius) * pi;
System.out.println("Area of circle with a radius of " + radius + "is: " + area);
}else if(option = 3){
System.out.println("Please enter a, or 0 if unknown (double): ");
double a = IO.readDouble();
System.out.println("Please enter b, or 0 if unknown (double): ");
double b = io.readDouble();
System.out.println("Please enter c, or 0 if unknown (double): ");
double b = IO.readDouble();
if(a == 0){
a = Math.sqrt(Math.pow(c, 2) - Math.pow(b, 2));
System.out.println("a: " + a + ", b:" + b + ", c: " + c);
}else if(b == 0){
b = Math.sqrt(Math.pow(c, 2) - Math.pow(a, 2));
System.out.println("a: " + a + ", b:" + b + ", c: " + c);
}else if(c == 0){
c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
System.out.println("a: " + a + ", b:" + b + ", c: " + c);
}else{
System.out.println("None of the sides are unknown! Uh-oh!");
}
}else{
System.out.println("Invalid number entered!");
}
}
1
Please submit any questions or corrections to joseph.a.boyle@rutgers.edu
You're Reading a Preview

Unlock to view full version