0% found this document useful (0 votes)
3 views2 pages

Lab 7

The document outlines three exercises for a programming lab. Exercise 1 involves creating a class with static fields and methods to demonstrate initialization. Exercise 2 requires simulating two villages sharing a well with a static variable for water amount, and Exercise 3 involves designing a MyPoint class for 2D points with various methods for manipulation and distance calculation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Lab 7

The document outlines three exercises for a programming lab. Exercise 1 involves creating a class with static fields and methods to demonstrate initialization. Exercise 2 requires simulating two villages sharing a well with a static variable for water amount, and Exercise 3 involves designing a MyPoint class for 2D points with various methods for manipulation and distance calculation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

LAB 7

Exercise (1): Create a class with a static String field that is


initialized at the point of definition, and another one that is
initialized by the static block. Add a static method that prints
both fields and demonstrates that they are both initialized before
they are used.
Exercise (2): Write one program to simulate process for two
villages sharing a well. Create Village class which defines one
static int member variable waterAmount, which is used to
represent the amount of water in a well. Then in the demo class
land two object instances for villages are created, one village
can change the value of the waterAmount, and the other village
can view the value of the waterAmount.

Exercise (3):
A class called MyPoint, which models a 2D point with x and y
coordinates, is designed as follows:
1) Two instance variables x (int) and y (int).
2) A default (or "no-arg") constructor that construct a point at
the default location of (0, 0).
3) A overloaded constructor that constructs a point with the
given x and y coordinates.
4) A method setXY() to set both x and y.
5) A method getXY() which returns the x and y in a 2-element
int array.
6) A toString() method that returns a string description of the
instance in the format "(x, y)".
7) A method called distance(int x, int y) that returns the distance
from this point to another point at the given (x, y) coordinates
8) An overloaded distance(MyPoint another) that returns the
distance from this point to the given MyPoint instance (called
another)
9) Another overloaded distance() method that returns the
distance from this point to the origin (0,0)
Develop the code for the class MyPoint. Also develop a JAVA
program (called TestMyPoint) to test all the methods defined in
the class

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy