% cd % cd cs21 % mkdir hw1 % cd hw1 % pwd /home/your_user_name/cs21/hw1/Also, you can use the in-class Java source code files as a starting point for assigments (just copy them from your week1 subdirectory into your hw1 subdirectory into a file with the approriate name).
# For example, from your hw1 subdirectory: % pwd /home/your_user_name/cs21/hw1/ % cp ../week1/InputExample.java Volume.javaNow just change the class name to Volume inside Volume.java file and modify the contents of the file to implement your solution to the Volume program.
Please complete the following 3 programs and turn them in using cs21handin. You should work on this assignment by yourself (no partners).
Remember to print out a prompt to the user to enter the input values, and then to print out the result in a way that would make sense to a user of your program. For example, here is the output of my program when a user enters 2.3 as the radius:
% java Volume This program computes the volume of a sphere. Enter the radius: 2.45 The volume of a sphere with radius 2.45 is 61.60082031833334Remember to get your program to read input, you need to create and use a new Scanner object:
import java.util.*; // add this to the top of your file before the class // definition Scanner reader; // inside main, declare a Scanner reference variable reader = new Scanner(System.in); // create a new Scanner object to read from stdin // then you can use methods of Scanner class to read in values r = reader.nextDouble(); // read in a double and assign it to r x = reader.nextInt(); // read in an int and assign it to x f = reader.nextFloat(); // read in a float and assign it to f To compile a Java program: -------------------------- % javac Volume.java To run a Java program: ---------------------- % java Volume
% java Convert This program reads in a total number of seconds time value entered by the user and converts it to days:hours:mins:secs Enter the total number of seconds: 61 61 seconds is 0 days 0 hours 1 mins 1 secs % java Convert This program reads in a total number of seconds time value entered by the user and converts it to days:hours:mins:secs Enter the total number of seconds: 432001 432001 seconds is 5 days 0 hours 0 mins 1 secs % java Convert This program reads in a total number of seconds time value entered by the user and converts it to days:hours:mins:secs Enter the total number of seconds: 443108 443108 seconds is 5 days 3 hours 5 mins 8 secs