Programming Problem 1 For this problem, follow the instructions for Program #5 i
ID: 3758945 • Letter: P
Question
Programming Problem 1 For this problem, follow the instructions for Program #5 in Chapter 3 Exercises of your textbook. When gasoline is $100 per barrel, then the consumer’s price at the pump is between $3.50 and $4.00 per gallon. Create a class named GasPrices. Its main() method holds an integer variable named pricePerBarrel to which you will assign a value entered by a user at the keyboard. Create a method to which you pass pricePerBarrel. The method displays the range of possible prices per gallon. For example, if gas is $120 per barrel, then the price at the pump should be between $4.20 and $4.80. Save the application as GasPrices.java Programming Problem 2 For this problem, follow the instructions for Program #12 in Chapter 3 Exercises of your textbook. Remember the following points: Create a class named Student. Student has fields for an ID number, number of credit hours earned, and number of points earned. (For example, many schools compute grade point averages based on a scale of 4, so a three-credit-hour class in which a student earns an A is worth 12 points.) Include methods to assign values to all fields. Student also has a field for grade point average. Include a method to compute the grade point average field by dividing points by credit hours earned. Write methods to display the values in each Student field. Save this class as Student.java Write a class named ShowStudent that instantiates a Student object from the class you created and assigns values to its fields. Compute the Student grade point average, and then display all the values associated with the Student. Save the application as ShowStudent.java Create a constructor for the Student class you created. The constructor should initialize each Student's ID number to 9999, his or her points earned to 12, and credit hours to 3 (resulting in a grade point average of 4.0). Write a program that demonstrates that the constructor works by instantiating an object and displaying the initial values. Save the application as ShowStudent2.java Please submit the source code for your program along with a word document that includes screen shots of the compiled results and executed output. The page margins on the top, bottom, left side, and right side should be 1 inch each.
Explanation / Answer
/**Java class to print the low and high price of gallon
* for given price per barrel
* */
//GasPrices.java
import java.util.Scanner;
public class GasPrices
{
public static void main(String[] args)
{
Scanner scanner=new Scanner(System.in);
System.out.println("Enter price of barrel ? ");
//read price per barrel
int pricePerBarrel=scanner.nextInt();
/*Calling static method that prints the price per gallon
at pump */
calculateGallonHighLowPrice(pricePerBarrel);
}
/**The method takes an integer value and calculates the price
* at the pump in the range of the price per gallon .
* for every 100 $ dollar barrel , the price should be in
* the range of 3.5 $ to 4.0 $ per barrel.*/
private static void calculateGallonHighLowPrice(int pricePerBarrel)
{
double lowPricePerBarrel=3.50;
double highPricePerBarrel=4.0;
//calculate low price per gallong for pricePerBarrel $ barrel with respect
//to 100 dollar barrel
double lowPricePerGallon=(pricePerBarrel/100.0)*lowPricePerBarrel;
//calculate high price per gallong for pricePerBarrel $ barrel with respect
//to 100 dollar barrel
double highPricePerGallon=(pricePerBarrel/100.0)*highPricePerBarrel;
System.out.println("Price at pump in the range of $"+lowPricePerGallon+
" and $"+highPricePerGallon);
}//end of the method
}//end of the class
----------------------------------------------------------------------------------------------------
Sample output:
Enter price of barrel ?
120
Price at pump in the range of $4.2 and $4.8
------------------------------------------------------------------------------------------------------------------
//Student.java
public class Student
{
//instance variables of class
private int studentid;
private double creditHours;
private double numberOfPoints;
//default constructor
public Student()
{
this.studentid=0;
this.creditHours=0.0;
this.numberOfPoints=0.0;
}
//parameter constructor
public Student(int studentid,
double creditHours, double numberOfPoints)
{
this.studentid=studentid;
this.creditHours=creditHours;
this.numberOfPoints=numberOfPoints;
}
//Set id of student
public void setID(int studentid)
{
this.studentid=studentid;
}
//Set method to set credit hours of student
public void setCreditHours(double creditHours)
{
this.creditHours=creditHours;
}
//Set method to set points earned
public void setNumberOfPoints(double numberOfPoints)
{
this.numberOfPoints=numberOfPoints;
}
//Returns the id of student
public int getID()
{
return studentid;
}
//Returns credit hours
public double getCreditHours()
{
return creditHours;
}
//Returns number of points
public double getNumberOfPoints()
{
return numberOfPoints;
}
//Returns average grade point
public double getAverage()
{
return numberOfPoints/creditHours;
}
}
------------------------------------------------------------------------------------------------------------------------
/*The class ShowStudent that instantiates Student
* class with id, credit hours, points earned and prints
* the grade average.*/
//ShowStudent.java
public class ShowStudent
{
public static void main(String[] args)
{
//Create Student object
Student student=new Student();
//set id, credit hours, and points earned
student.setID(9999);
student.setCreditHours(3);
student.setNumberOfPoints(12);
//print details of students
System.out.println("Student Details");
System.out.println("ID : "+student.getID());
System.out.printf("Credit Hours :%.2f ",student.getCreditHours());
System.out.printf("Points earned:%.2f ",student.getNumberOfPoints());
//print average grade points
System.out.printf("Average grade:%.2f ",student.getAverage());
}
}
-----------------------------------------------------------------------------------------------------------------------
Sample Output:
Student Details
ID : 9999
Credit Hours :3.00
Points earned:12.00
Average grade:4.00
--------------------------------------------------------------------------------------------------------------------------------------------
/*The class ShowStudent2 that Student
* class set methods with id, credit hours, points earned and prints
* the grade average.*/
//ShowStudent2.java
public class ShowStudent2
{
public static void main(String[] args)
{
//Create an instance of Student clas with id, credit hours
//points earned
Student student=new Student(9999,3,12);
//print student details
System.out.println("Student Details");
System.out.println("ID : "+student.getID());
System.out.printf("Credit Hours :%.2f ",student.getCreditHours());
System.out.printf("Points earned:%.2f ",student.getNumberOfPoints());
System.out.printf("Average grade:%.2f ",student.getAverage());
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Sample output:
Student Details
ID : 9999
Credit Hours :3.00
Points earned:12.00
Average grade:4.00
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.