Update all of your programs for Programming Assignment 1 ( Science & Engineering
ID: 3863182 • Letter: U
Question
Update all of your programs for Programming Assignment 1 (Science & Engineering Problems) and Programming Assignment 2 (More Science and Engineering Problems) so that input validation is implemented as follows:
Programming Assignment 1: Science & Engineering Problems
Program 1:
No resistance (none of R1, R2 or R3) can be negative
Program 2:
Relative Humidity (RH) must be between 0 and 1
Program 3:
Neither charge (Q1 nor Q2) can be negative
Programming Assignment 2: More Science and Engineering Problems
Program 1 (Engineering: acceleration):
Neither velocity (v0 nor v1) can be negative
The time span (t) must be positive
Program 2: (Science: calculating energy):
The amount of water (M) cannot be negative
Program 3: (Engineering: finding runway length):
Speed in meters/second (v) cannot be negative
Acceleration in m/s2 (a) cannot be 0 nor negative
Program 4: (Science: wind-chill temperature):
Temperature in degrees Fahrenheit (ta) must be between -58.0 and 41.0
Wind speed in MPH (v) must be at least 2
Make sure:
to include all Design Recipe documentation included in your original submission,
that your examples are updated to include invalid input (if not included already), and
that your algorithms are updated to include the input validation steps, if these were not included in the original algorithms.
Extra Credit (+25 points)
It turns out that the Scanner class includes input validation (boolean) methods for the various data types. The names of these methods are formed by adding the "has" prefix to the input method name. For example, the Scanner method nextInt() reads an integer from the keyboard; the Scannerboolean method hasNextInt() validates the input and returns true if an integer has been input or false otherwise.
Since the Scanner "has" methods are boolean, they are typically used as a condition of an if...else statement. However, their use is a little strange, in that the validation must be done before the input. For example, if:
price has been declared as a double variable
kbd has been declared/created as a Scanner for the keyboard (i.e., System.in)
Then, to validate the input for the price variable as a positive double via the kbd Scanner, we write:
System.out.print("Enter the price: ")
if (kbd.hasNextDouble()) // validate that a double value is input for price
{
price = kbd.nextDouble();
}
else
{
System.err.println("Invalid price; good-bye...");
System.exit(0);
}
// validate that price is positive
if (!(price > 0))
{
System.err.println("Invalid price; good-bye...");
System.exit(0);
}
Explanation / Answer
Program 1:
No resistance (none of R1, R2 or R3) can be negative
import java.util.Scanner;
public class Resistance {
public static void main(String args[]){
int r1,r2,r3;
Scanner sc = new Scanner(System.in);
System.out.println("Enter Resistance r1");
r1 = sc.nextInt();
if(r1<0)
{
System.out.println("Value of resistance cannot be negative."
+ "So exiting");
System.exit(0);
}
System.out.println("Enter Resistance r2");
r2 = sc.nextInt();
if(r2<0)
{
System.out.println("Value of resistance cannot be negative."
+ "So exiting");
System.exit(0);
}
System.out.println("Enter Resistance r3");
r3 = sc.nextInt();
if(r3<0)
{
System.out.println("Value of resistance cannot be negative."
+ "So exiting");
System.exit(0);
}
}
}
Program 2:
Relative Humidity (RH) must be between 0 and 1
import java.util.Scanner;
public class Humidity {
public static void main(String args[]){
double Rh;
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of Relative humidity");
Rh= Double.parseDouble(sc.next());
if(Rh<=0 || Rh>=1){
System.out.println("value of Relative humodity should be between 0 and 1");
System.exit(0);
}
}
}
Program 3:
Neither charge (Q1 nor Q2) can be negative
import java.util.Scanner;
public class Charge {
public static void main(String[] args) {
// TODO Auto-generated method stub
double q1,q2;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of Q1");
q1 = Double.parseDouble(sc.nextLine());
if(q1<0){
System.out.println("Value of charge should not be negative"
+ "So exiting!");
System.exit(0);
}
System.out.println("Enter the value of Q2");
q2 = Double.parseDouble(sc.nextLine());
if(q2<0){
System.out.println("Value of charge should not be negative"
+ "So exiting!");
System.exit(0);
}
}
}
Programming Assignment 2: More Science and Engineering Problems
Program 1 (Engineering: acceleration):
Neither velocity (v0 nor v1) can be negative
The time span (t) must be positive
import java.util.Scanner;
public class Velocity {
public static void main(String[] args) {
// TODO Auto-generated method stub
double v0,v1,time;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of v0");
v0 = Double.parseDouble(sc.nextLine());
if(v0<0)
{
System.out.println("velocity cannot be negative");
System.exit(0);
}
System.out.println("Enter the value of v0");
v1 = Double.parseDouble(sc.nextLine());
if(v1<0)
{
System.out.println("velocity cannot be negative");
System.exit(0);
}
System.out.println("Enter the value of time span");
time = Double.parseDouble(sc.nextLine());
if(time<0)
{
System.out.println("time cannot be negative");
System.exit(0);
}
}
}
as per chegg policy we are required to answer only first 4 parts if there are multiple questions to be answered.
and also due to time contraint answerinf the first 4 parts
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.