The following class contains several errors that violate the

The following class contains several errors that violate the rules of Java. Describe each error and specify whether it is (a) lexical, (b) syntactic, or (c) semantic. Use the numbers shown to identify the line on which each error occurs. The class may also contain programming errors that do not violate the rules of Java and will not be detected by a Java compiler. You should ignore these errors.

1. class Thermometer private int temperature 3. public Thermometer (int degrees) temperature degrees; 6. public Thermometer temperature 0. O; 9. public void make (int degrees) Warmer 10 temperature degrees 12. public void make Cooler (int degrees) temperature degrees; 13. 14 15 public get Temperature 16. return temperature 18. public string tostring 19 return temperature degrees 20 21.

Solution

1.class Thermometer {

2.private int temperature;

//line 2.Syntax Error:In java every should ends with semicolon(;)private int temperature;         

3.public Thermometer(int degrees){

4.         temperature=degrees;

5.}

6.public Thermometer(){

7.         temperature=0.0;

//line 7.Semantic error: The type temperature and 0.0 is not compatible

8.}

9.public void makeWarmer(int degrees){

10.       temperature=+degrees;

//line 10.Lexical error : Here we should use += (temperature=temperature+degrees is temperature+=degrees)

11.}

12.public void makeCooler(int degrees){

13.       temperature-=degrees;

14.}

15.public getTemperature() {

//line 15.Syntax Error: As we are returning temperature of type int we should declare return type in method definition . we should declare like public int getTemperature()

16.       return temperature;

17.}

18.public string tostring(){

   //line 18.Semantic Error : \'S\' must be capital letter in string.String is correct

19.    return temperature+\' degrees\';

//line 19.Syntax Error: we should provide String in double quotes (\" degrees \") not in single quotes

20.}

21.}

__________________

a)lexical:

line 10

b)syntactic:

line 2,line 15,line 19

c)semantic:

line 7,line 18

______________Thank You

The following class contains several errors that violate the rules of Java. Describe each error and specify whether it is (a) lexical, (b) syntactic, or (c) sem
The following class contains several errors that violate the rules of Java. Describe each error and specify whether it is (a) lexical, (b) syntactic, or (c) sem

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site