I need help debugging. Joyce Farrells, Programming Design and Logic 8th Edition,
ID: 650367 • Letter: I
Question
I need help debugging. Joyce Farrells, Programming Design and Logic 8th Edition, Chapter 11.
This diagram describes a Sunglasses class and a Prescription on Sunglasses class that inherits from it. The Sunglasses class contains a string style and a numeric darkness value that is required to be a number from 1 through 4. The subclass also includes two numbers for OS (left eye) value and OD (right eye) value. These typically are numbers such as +3.0 (which ?s moderately farsighted) or -1.5 (which is slightly nearsighted),Explanation / Answer
//Class declaration for Sunglasses
//Sunglasses.java
public class Sunglasses
{
//private members of class Sunglasses
private String style;
private int darkness;
//default constructor
public Sunglasses()
{
;
darkness=0;
}
//parameterized constructor that sets style and darkness
public Sunglasses(String style, int darkness)
{
this.style=style;
this.darkness=darkness;
}
//Sets the style
public void steStyle(String style)
{
this.style=style;
}
//Returns style
public String getStyle()
{
return style;
}
//Sets the darkness
public void setDarkness(int darkness)
{
if(darkness>0 && darkness<=4)
this.darkness=darkness;
else
System.out.println("Invalid darkness");
}
//Returns the darkness
public int getDarkness()
{
return darkness;
}
}
---------------------------------------------------
//Class declaration for PrescriptionSunglasses
//PrescriptionSunglasses.java
//This class extends the super class Sunglasses
public class PrescriptionSunglasses
extends Sunglasses
{
//private members of class
private double os;
private double od;
//Default constructor
public PrescriptionSunglasses()
{
//call super class(Sunglasses) default constructor
super();
os=0;
od=0;
}
public PrescriptionSunglasses(String style, int darkness,double os, double od)
{
//calling super class(Sunglasses) argumented constructor to
//ste style and darkness
super(style, darkness);
//set os and od values
this.os=os;
this.od=od;
}
//Set osNum
public void setOs(double osNum)
{
this.os=osNum;
}
//Returns os number
public double getOs()
{
return os;
}
//Set odNum
public void setOd(double odNum)
{
this.od=odNum;
}
//Returns od number
public double getOd()
{
return od;
}
}
---------------------------------------------------
//Tester java program that test the class
//PrescriptionSunglasses
//Driver.java
public class Driver
{
public static void main(String[] args)
{
//Create an instance of PrescriptionSunglasses object
//with style, darkness ,left eye value and right eye values
PrescriptionSunglasses prescription=new PrescriptionSunglasses("Cool", 1, 3,-1.5);
//print prescription object
System.out.println("Presciption");
System.out.println("Style : "+prescription.getStyle());
System.out.println("Darkness : "+prescription.getDarkness());
System.out.println("Left eye : "+prescription.getOs());
System.out.println("Right eye : "+prescription.getOd());
}
}
---------------------------------------------------
Sample output:
Presciption
Style : Cool
Darkness : 1
Left eye : 3.0
Right eye : -1.5
Hope this helps you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.