Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have been stuck on two problems today. Can someone please Help?

**Problem 1**

https://i.stack.imgur.com/fic93.png
https://i.stack.imgur.com/ES2P4.png
https://i.stack.imgur.com/KmJrK.png
https://i.stack.imgur.com/PiwSH.png



**Code to be filled in:**

    class Rectangle {
        /* Enter your code here for the Rectangle class. */
        private double lowerLeftX, lowerLeftY;
        private double width, height;
      
        private static int decPlaces = 2;
        public static int getDecPlaces() { /* To be completed */ }
        public static void setDecPlaces(int decPlaces) { /* To be completed */ }
        }
    }
    
    class TestRectangle {
        /* Enter your code here for the TestRectangle class. */
      
        public static void main(String[] args) {
            /* To be completed */
        }
    }

______________________________

**Problem 2:**

https://i.stack.imgur.com/NOuC1.png
https://i.stack.imgur.com/ypN4u.png
https://i.stack.imgur.com/1OMaJ.png
https://i.stack.imgur.com/dJxbA.png

**Code to be filled in:**

    class Circle {
        /* Enter your code here for the Circle class. */
        private double centerX, centerY;
        private double radius;
      
        private static int decPlaces = 2;
        public static int getDecPlaces() { /* To be completed */ }
        public static void setDecPlaces(int decPlaces) { /* To be completed */ }
        }
    }
    
    class TestCircle {
        /* Enter your code here for the TestCircle class. */
      
        public static void main(String[] args) {
            /* To be completed */
        }
    }

_________________________

I would kindly thank anyone for assistance with these two problems.


What I have tried:

For the first problem I have this for my code.

import java.util.*;

class Rectangle {

private int x;

private int y;

private int width;

private int height;

//default constructor

public Rectangle()

{

x=0;

y=0;

width=0;

height=0;

}

//parameterised constructor

public Rectangle(int x, int y, int width, int height) {

set(x,y,width,height);

}

public void set(int x, int y, int width, int height)

{

/* replace any negative distance parameters with zero.*/

if(width<0)

width=0;

if(height<0)

height=0;

this.x = x;

this.y = y;

this.width = width;

this.height = height;

}

public static String info()

{

return "Student Name";

}

//getters

public int getX() {

return x;

}

public int getY() {

return y;

}

public int getWidth() {

return width;

}

public int getHeight() {

return height;

}


public String toString() {

return String.format("base: (%d,%d) w:%d h:%d",x,y,width,height);

}

public int area()

{

return height*width;

}

public int perimeter()

{

return 2*(width+height);

}

public boolean contains(Rectangle rectangle)

{

return (rectangle.x+ rectangle.width)<(this.x+this.width)&&

(rectangle.x)>(this.x)&&

(rectangle.y)>(this.y)&&

(rectangle.y+ rectangle.height)<(this.y+this.height);

}

}

class Solution {

public static void main(String[] args)

{

Rectangle rectangle1 = new Rectangle(3,4,10,12);

Rectangle rectangle2 = new Rectangle(-2,-5,9,4);

Rectangle.info();

System.out.println(rectangle1);

System.out.println(rectangle2);

System.out.println("Area of Rectangle1: "+ rectangle1.area());

System.out.println("Area of Rectangle2: "+ rectangle2.area());

System.out.println("Perimeter of Rectangle1: "+ rectangle1.perimeter());

System.out.println("Perimeter of Rectangle2: "+ rectangle2.perimeter());

System.out.println("rectangle1 contains rectangle2: "+rectangle1.contains(rectangle2));

}

}
Posted
Updated 17-Jul-22 18:25pm
Comments
Richard MacCutchan 18-Jul-22 3:49am    
You have not explained what the problem is.

1 solution

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Comments
Ho_oT 18-Jul-22 0:38am    
I have listed my code for the Rectangle Problem. It is giving me an output of (below):

base: (3,4) w:10 h:12
base: (-2,-5) w:9 h:4
Area of Rectangle1: 120
Area of Rectangle2: 36
Perimeter of Rectangle1: 44
Perimeter of Rectangle2: 26
rectangle1 contains rectangle2: false

No where near the expected output. I am in need my friend.
OriginalGriff 18-Jul-22 1:17am    
Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900