5A
Write a program called TetrahedronPrinter
that will compute the surface area and the height of a regular tetrahedron given the length of its side. A regular tetrahedron has 4 faces and all the edges are the same length. The formulas and a picture are here. (Scroll down to find the formulas)
Ask the user to input the edge length using Scanner class. (Use the prompt, "Enter the edge length: ") Test that the input is non-negative. If it is negative, print the message "Edge can not be negative". Otherwise, calculate and print both surface area and height formatted to 2 decimal places. Use printf. Do not round with some other method.
5B
We do not want users of our classes to be able to set invalid values for instance variables. Now that you know how to use if statements, you are going to add error checking to a a few classes from earlier homeworks.
You should not print error messages from constructors or methods of object. In the real world, you might throw an exception when the parameter is invalid, but we have not covered exceptions yet. We will just be sure the data in our objects are valid. Follow the directions below to add error checking to each class.
Use proper if structure. Do not do unnecessary tests . Do not have empty if or else blocks
You can get working versions of any of these from the solutions posted on laughton.com/obrien/sjsu.
HeliumBalloon
(hw 4a)
setRadius()
, if the radius is < 0, set the radius to 0WaterFlask
(hw3b)
drink
method do not let the amount go below 0. If the user tries to drink more than is in the water flask, set the amount in the flask to 0.Climber
(hw 3a)
climb()
method, do not allow the child to go over the top of the pole (10 units). If this climb would put the child over the top, do not do anything. Remember not to have an empty if or else clause. Use a constant for the top of the pole. Call it POLE_TOPpublic static final int POLE_TOP = 10;
setName()
and call setName
from the constructor.For the draft, make the changes to HeliumBalloon
5C
Write a class called MyNumber
that represents a positive number. MyNumber
has a constructor that takes an integer.
Provide the constructor:
public MyNumber(int value)
if value is negative, multiply if by -1 so that you have a positive number. Provide methods
public int getNumber()
gets this numberpublic void setNumber(int value)
sets a new number. if value is negative, multiply if by -1 so that you have a positive number. In final, have the constructor call setNumber so you do not write the validity check code twice.public int digitCount()
gets the number of digits in this number. Hint: you can convert a number to a string my concatenating it with the empty string.public String formatWithCommas()
gets a string representing the number in the comma format, if the number is less than 1,000,000. If the number is greater than or equal to 1,000,000, return the string "too big"For the draft, provide, the instance variable, the constructor and the getNumber method.