Question

1. The following class contains several errors that violate the rules of Java: 1. class Part t 2. private int number; 3. private String name; 4. private int quantity; 5. public Part (int number, String name, int quantity) 6. 7. 8. this.number number; this.name-name this quantity = quantity; 10. public Part (int number, string name) this (number, name, 0); 12. 13. public Part) 14 15. this (0, No name) 16. public void decreaseQuantity(int amount) 17 quantityamount; 18. 19. public int getName () 20. return name; 21 22. public int getnumber ) 23. return number; 24. 25. public int getQuantity) 26 return quanity; 27. 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 de- tected by a Java compiler. You should ignore these errors

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer: The program contains Errors at following Lines

Line 14 it is lexical error because the character literal is unclosed

Line 14 It is also Syntax Error becase it is not a statement

Line 21 we are getting incompatable type so it is Semantic Error

Program:

class Part {
private int number;
private String name;
private int quantity;
public Part(int number,String name,int quantity) {
this.number=number;
this.name=name;
this.quantity=quantity;
}
public Part(int number,String name) {
this(number,name,0);
}
public Part() {
this(0, 'No name');// lexical error because the character literal is unclosed
// It is also Syntax Error becase it is not a statement

}
public void decreaseQuantity(int amount) {
quantity=-amount;
}
public int getName() {
return name; // Here we are getting incompatable type so it is Semantic Error
}
public int getnumber() {
return number;
}
public int getQuantity() {
return quantity;
}
}

Output:

Add a comment
Know the answer?
Add Answer to:
The following class contains several errors that violate the rules of Java: class Part ( private...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Do 72a, 72b, 72c, 72d. Code & output required. public class Employee { private int...

    Java Do 72a, 72b, 72c, 72d. Code & output required. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...

  • 4. Command pattern //class Stock public class Stock { private String name; private double price; public...

    4. Command pattern //class Stock public class Stock { private String name; private double price; public Product(String name, double price) { this.name = name; this.price = price; } public void buy(int quantity){ System.out.println(“BOUGHT: “ + quantity + “x “ + this); } public void sell(int quantity){ System.out.println(“SOLD: “ + quantity + “x “ + this); } public String toString() { return “Product [name=” + name + “, price=” + price + “]”; } } a. Create two command classes that...

  • JAVA help Create a class Individual, which has: Instance variables for name and phone number of...

    JAVA help Create a class Individual, which has: Instance variables for name and phone number of individual. Add required constructors, accessor, mutator, toString() , and equals method. Use array to implement a simple phone book , by making an array of objects that stores the name and corresponding phone number. The program should allow searching in phone book for a record based on name, and displaying the record if the record is found. An appropriate message should be displayed if...

  • This is my playlist class: class Playlist{       private String name;    private int numberOfRecordings...

    This is my playlist class: class Playlist{       private String name;    private int numberOfRecordings = 0;    private int durationInSeconds = 0;    private int MAX_PLAYLIST_SIZE;    Recording recordingslist[];       Playlist(){        name = "Unknown";        MAX_PLAYLIST_SIZE = 5;        recordingslist = new Recording[MAX_PLAYLIST_SIZE];        durationInSeconds = 0;    }       Playlist(String name, int size){        this.name = name;        this.MAX_PLAYLIST_SIZE = size;        recordingslist = new Recording[MAX_PLAYLIST_SIZE];   ...

  • Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java...

    Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java //package simple; public class PartTest { public static void main(String[] args) { ArrayList<ExpendablePart> system=new ArrayList<ExpendablePart>();   // creating Part Object Part part1 = new ExpendablePart("Last, First"); part1.setNumber("AX-34R"); part1.setNcage("MN34R"); part1.setNiin("ABCD-RF-WDE-KLJM"); // printing information System.out.println(part1.toString());       //Create a part2 object of class Part Part part2=new ConsumablePart("Widget, purple"); part2.setNumber("12345"); part2.setNcage("OU812"); part2.setNiin("1234-12-123-1234");    // printing information System.out.println(part2.toString());    //checking equality of two Part class objects if(part1.equals(part2)) System.out.println("part1 and...

  • Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring;...

    Assignment (to be done in Java): Person Class: public class Person extends Passenger{ private int numOffspring; public Person() {    this.numOffspring = 0; } public Person (int numOffspring) {    this.numOffspring = numOffspring; } public Person(String name, int birthYear, double weight, double height, char gender, int numCarryOn, int numOffspring) {    super(name, birthYear, weight, height, gender, numCarryOn);       if(numOffspring < 0) {        this.numOffspring = 0;    }    this.numOffspring = numOffspring; } public int getNumOffspring() {   ...

  • Java. Java is a new programming language I am learning, and so far I am a...

    Java. Java is a new programming language I am learning, and so far I am a bit troubled about it. Hopefully, I can explain it right. For my assignment, we have to create a class called Student with three private attributes of Name (String), Grade (int), and CName(String). In the driver class, I am suppose to have a total of 3 objects of type Student. Also, the user have to input the data. My problem is that I can get...

  • I have a java class that i need to rewrite in python. this is what i...

    I have a java class that i need to rewrite in python. this is what i have so far: class Publisher: __publisherName='' __publisherAddress=''    def __init__(self,publisherName,publisherAddress): self.__publisherName=publisherName self.__publisherAddress=publisherAddress    def getName(self): return self.__publisherName    def setName(self,publisherName): self.__publisherName=publisherName    def getAddress(self): return self.__publisherAddress    def setAddress(self,publisherAddress): self.__publisherAddress=publisherAddress    def toString(self): and here is the Java class that i need in python: public class Publisher { //Todo: Publisher has a name and an address. private String name; private String address; public Publisher(String...

  • Solve this using Java for an Intro To Java Class. Provided files: Person.java /** * Models...

    Solve this using Java for an Intro To Java Class. Provided files: Person.java /** * Models a person */ public class Person { private String name; private String gender; private int age; /** * Consructs a Person object * @param name the name of the person * @param gender the gender of the person either * m for male or f for female * @param age the age of the person */ public Person(String name, String gender, int age) {...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT