Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part a) Create a class called MyDegree. It should have two variables of type dou

ID: 3559085 • Letter: P

Question

Part a)

Create a class called MyDegree. It should have two variables of type double. One called fahrenheit and the other celsius. It should have one constructor that takes in two variables, the first of type double the second of type String. Depending on the value of the String parameter (which should be either fahrenheit or celsius) set the corresponding variable to the value passed as a parameter. Compute the value for the other variable. (Hint: Fahrenheit to Celsius: f = (9/5)*c +32, Celsius to Fahrenheit: c = (5/9)*(f-32). Create 2 get methods, one for each variable. Create 2 set methods, one for each variable (don

Explanation / Answer

please rate

import java.text.DateFormat;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.JOptionPane;

class MyDegree {
  
   private double fahrenheit,celsius;
  
   private void calculateF(){
       fahrenheit = (9.0/5)*celsius + 32.0;
   }
  
   private void calculateC(){
       celsius = (5.0/9)*(fahrenheit - 32.0);
   }
  
   public MyDegree(double temperature,String type){
       if(type.equalsIgnoreCase("celsius")){
           celsius = temperature;
           calculateF();
       }else{
           fahrenheit = temperature;
           calculateC();
       }
   }
  
   public void setFahrenheit(double fahrenheit){
       this.fahrenheit = fahrenheit;
       calculateC();
   }
  
   public void setCelsius(double celsius){
       this.celsius = celsius;
       calculateF();
   }
  
   public double getFahrenheit(){
       return fahrenheit;
   }
  
   public double getCelsius(){
       return celsius;
   }
}

class MyDate {
  
   private int date,month,year;
   private MyDegree high,low;
  
   public boolean isValid(int date, int month, int year){
       try {
           String inputDate = date+"-"+month+"-"+year;
            DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
            formatter.setLenient(false);
            formatter.parse(inputDate);
            return true;
       } catch (ParseException e) {
            return false;
       }
   }
  
   public MyDate(int date, int month, int year){
       if(isValid(date,month,year)){
           this.date = date;
           this.month = month;
           this.year = year;
       }else{
           this.date = 1;
           this.month = 1;
           this.year = 2000;
           JOptionPane.showMessageDialog(null, "Invalid date. Default date used.");
       }
   }
  
   public MyDate getNextDate(){
       Calendar c;
       c = Calendar.getInstance();
       c.set(year, month, date);
       c.add(Calendar.DATE, 1);
       return new MyDate(c.get(Calendar.DATE),c.get(Calendar.MONTH),c.get(Calendar.YEAR));
   }
  
   public void setHigh(double temperature, String type){
       high = new MyDegree(temperature, type);
   }
  
   public void setLow(double temperature, String type){
       low = new MyDegree(temperature, type);
   }
  
   public int getYear(){
       return year;
   }
  
   public int getMonth(){
       return month;
   }
  
   public int getDate(){
       return date;
   }
  
   public int getLowC(){
       return (int)low.getCelsius();
   }
  
   public int getLowF(){
       return (int)low.getFahrenheit();
   }
  
   public int getHighC(){
       return (int)high.getCelsius();
   }
  
   public int getHighF(){
       return (int)high.getFahrenheit();
   }

}

public class Main{
   public static void main(String[] args){
       MyDate[] m = new MyDate[5];
       int result;
       do{
           int date = Integer.parseInt(JOptionPane.showInputDialog("Enter date"));
           int month = Integer.parseInt(JOptionPane.showInputDialog("Enter month"));
           int year = Integer.parseInt(JOptionPane.showInputDialog("Enter year"));
           m[0] = new MyDate(date,month,year);
           result = JOptionPane.showConfirmDialog(null, "Is the Date correct(dd-MM-yyyy): "+m[0].getDate()+"-"+m[0].getMonth()+"-"+m[0].getYear());
       }while(result!=JOptionPane.YES_OPTION);
       m[1] = m[0].getNextDate();
       m[2] = m[1].getNextDate();
       m[3] = m[2].getNextDate();
       m[4] = m[3].getNextDate();
      
      /*

Set the temperatures here and use system.out.println to print the output

*/
      
   }
}