Question

List and explain the logical Error in the following program. Then submit the correct .java program....

List and explain the logical Error in the following program. Then submit the correct .java program.

public class square

{

private int sideLength;

private int area;

public Square(int initialLength)

{

sideLength = initialLength;

area = sideLength * sideLength;

}

public int area(){ return area;}

public void grow(){ sideLength = 2* sideLength;}

}

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

We should not calculate the area of square in the constructor as it is called only once. And if the length of square changes after the class is initialised then the area wont be changed

Correct Program:

public class square
{
private int sideLength;
private int area;
public square(int initialLength)
{
sideLength = initialLength;
}
public int area(){ return sideLength*sideLength;}
public void grow(){ sideLength = 2* sideLength;}
}

Add a comment
Know the answer?
Add Answer to:
List and explain the logical Error in the following program. Then submit the correct .java program....
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
  • In Java Create a testing class that does the following to the given codes below: To...

    In Java Create a testing class that does the following to the given codes below: To demonstrate polymorphism do the following: Create an arraylist to hold 4 base class objects Populate the arraylist with one object of each data type Code a loop that will process each element of the arraylist Call the first ‘common functionality’ method Call the second ‘common functionality’ method Call the third ‘common functionality’ method Verify that each of these method calls produces unique results Call...

  • JAVA- Complete the code by following guidelines in comments. class CircularList { private Link current; private...

    JAVA- Complete the code by following guidelines in comments. class CircularList { private Link current; private Link prev; public CircularList() { // implement: set both current and prev to null } public boolean isEmpty() { // implement return true; } public void insert(int id) { // implement: insert the new node behind the current node } public Link delete() { // implement: delete the node referred by current return null; } public Link delete(int id) { // implement: delete the...

  • FOR JAVA: Summary: Create a program that adds students to the class list (see below). The...

    FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...

  • Analyze the following code in these 2 Java classes and choose the correct statement below: public...

    Analyze the following code in these 2 Java classes and choose the correct statement below: public class Test { public static void main(String[] args) { Time1 time = new Time1(); time.print(); } } public class Time1 {    private String showTime;    public Time1(String newTime) { showTime = newTime; }    public void print() { System.out.printf("%s", showTime); } } a. The program will compile and run if you change: Time1 time = new Time1();  → Time1 time = new Time1("5:15"); b....

  • Identify a logical error in the following code and fix it. public class test1 {    ...

    Identify a logical error in the following code and fix it. public class test1 {     public static void main(String[] args){         int total;         float avg;         int a, b, c;         a=10;         b=5;         c=2;         total=a+b+c;         avg=total/3;         System.out.println("Average: "+avg);     } } Answer: Write the output of the program below. import java.util.Scanner; public class test2 { public static void main(String[] arg){ int psid; String name; Scanner input=new Scanner(System.in); System.out.println("Enter your PSID"); psid=input.nextInt(); System.out.println("Enter your...

  • 1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program!...

    1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program! 3     public static void main(String[] args) { 4         System.out.println("Hello, World!"); 5     } 6 } What is on line 1? a. a variable declaration b. a statement c. a method (subroutine) definition d. a comment e. a class definition 2) Which one of the following does NOT describe an array? a. It can be used in a for-each loop. b. It has a numbered sequence...

  • USE JAVA PROGRAMMING Create a program that would collect list of persons using double link list...

    USE JAVA PROGRAMMING Create a program that would collect list of persons using double link list and use a Merge Sort to sort the object by age. Create a class called Person : name and age Create methods that add, and delete Person from the link list Create a method that sorts the persons' objects by age. package mergesort; public class MergeSortExample { private static Comparable[] aux; // auxiliary array for merges public static void sort(Comparable[] a) { aux =...

  • The following class contains several errors that violate the rules of Java: class Part ( private...

    The following class contains several errors that violate the rules of Java: 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'): } public void decreaseQuantity(int amount) { quantity = -amount: } public int getName () { return...

  • JAVA LANGUAGE create an empty Java file called Date build it to find any syntax errors...

    JAVA LANGUAGE create an empty Java file called Date build it to find any syntax errors and eliminate them identify each member of this class and write your answer in a comment There are four different types of class member: constant, instance variable, constructor, and method identify a method as class method if it is static /* * Java program: Date.java * * Define the class Date */ import java.time.LocalDateTime; public class Date { private static final int daysEachYear =...

  • write a java program that implements the following state diagram Write a Java program that implements...

    write a java program that implements the following state diagram Write a Java program that implements the following state diagram. Turn in a listing of your program, and the results of a test run with n = 97, You will need to know how to do input in Java. Use the following construct and include the given class in your progr int (new IntReader ) .readInt (O: import java.io.*; // a class to read in an integer public class IntReader...

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
ADVERTISEMENT