0% found this document useful (0 votes)
22 views

ICS102 - Lab8 - Defining Classes

This lab covers static variables and methods in Java as well as commonly used methods from the Math class. Students will learn that static variables have one copy per class rather than per object. Static methods cannot access non-static variables or methods unless an object is created. The Math class contains useful methods like abs(), max(), min(), pow(), sqrt(), floor(), and ceil() that perform basic math operations without needing an import statement. Students will practice these concepts by correcting code examples and creating a Sphere class with static and non-static elements.

Uploaded by

tvdxv7bjj8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

ICS102 - Lab8 - Defining Classes

This lab covers static variables and methods in Java as well as commonly used methods from the Math class. Students will learn that static variables have one copy per class rather than per object. Static methods cannot access non-static variables or methods unless an object is created. The Math class contains useful methods like abs(), max(), min(), pow(), sqrt(), floor(), and ceil() that perform basic math operations without needing an import statement. Students will practice these concepts by correcting code examples and creating a Sphere class with static and non-static elements.

Uploaded by

tvdxv7bjj8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab 8: Defining Classes II

Objectives:
By completing this lab, students should be able to:
 Static variables and methods.
 The Math class.
Syntax Reference with examples:
Following are the syntaxes for the required commands.

Static Variables In Java

Syntax:

static datatype variableName;


 There is only one copy of a static variable per class, unlike instance
variables where each object has its own copy
Example:
Static int x=100;

Static Methods in Java

Syntax:

public static methodtype methodName(parameters){ . . . }

 A static method cannot refer to a non-static variable of the class. (unless


it creates a new object of the class and use that object as calling object)
Example 1:
Class Exercise{

Static int var1;


int var2;
static double addition(){
double add= var1+var2; //illegal because var2 is not static variable
return add; }
Example 2:
Class Exercise{
Static int var1;
int var2;
static double addition(Exercise E){
double add= var1 + E.var2;
/*legal because E is an object of Exercise class and it is used to use var2
which is a none-static variable*/
return add; }

 A static method cannot invoke a non-static method of the class (unless it


creates a new object of the class and use that object as calling object)
Math Class Methods

 It is found in the java.lang package, so it does not require an import


statement.

 It contains methods for performing basic numeric operations such as the


elementary exponential, logarithm, square root, and trigonometric functions.

 Some Methods in Math Class:

Method Name Description Example


static int abs(int a) returns the Math.abs(-2) Returns 2
absolute value of
an int value.
static double abs(double a) returns the Math.abs(-2.3) Returns 2.3
absolute value of
a double value
static int max(int a, int b) returns the Math.max(6,4) Returns 6
. greater of two int
values Math.max(-4,-7) Returns -4
static double min(double a, returns the Math.min(6.6,4.7) Returns 4.7
double b) smaller of two
double values.

static int min(int a, int b) returns the Math.min(10,23) Returns 10


smaller of two int
values.

static double pow(double a, returns the value Math.pow(2.0,3.0) Returns 8.0


double b) of the first
argument raised to
the power of the
second argument.
static double sqrt(double a) returns the Math.sqrt(25.0) Returns 5.0
correctly rounded
positive square
root of a double
value.
static double floor(double returns the Math.floor(3.9) Returns 3
Instructions:
a) largest double
value that is less
Task # 1: Creating a new Project in JCreator.
than or equal to
1. Find the JCreator icon in your PC. the
It should on Desktop or on C Driver. Then double click on the
argument.
JCreator icon. Math.ceil(3.1) Returns 4
static
2. Go double ceil(double
to File New Project.a) returns the
smallest double
3. Select Empty Java Project. Then Click
value Next button.
that is
4. Write the Project Name “Lab8”. Then choose than
greater the location
or e.g. ICS102Labs folder on the Desktop.
Then Click Next  Finish. equal to the
argument.

Exercise
 All of1:its methods and data are static; therefore, they are invoked with the
class name Math instead of a calling object
Correct the following java code. Highlight the errors and identify the reasons. Try to solve it on
paper before using JCreator.
Exercise 1 :

Correct the following java code . highlight the errors and identify the reasons

import java.util.Math;

class MathTest {
public static void main(String[] args) {

Math obj = new Math();


int number;

obj.abs(-9);
System.out.println(math.floor(6.9));
number = math.max(-3, -7);
System.out.println(number);
number= obj.sqrt(16);

}
}

Exercise 2:

1. Create a new Java file named exercise2 and add it to the lab 8 project.
2. Define a class named Sphere that contains:
a. A double variable named radius.
b. A String variable named name.
c. A default constructor to initialize the radius and name to 0.
d. A parameterized constructors to initialize the radius and the name of the sphere.
e. Mutator methods for the radius and name.
f. Accessor methods for the radius and name.
g. A method named sphereVolume with no parameters to compute and then return the volume
3
of the sphere according to the formula 𝑉𝑜𝑙𝑢𝑚𝑒 = 𝜋𝑟 3
4
- In this Java class named Sphere, the specified requirements are fulfilled:
1. It contains a double variable radius and a String variable name

2. It has a default constructor that initializes radius and name to 0 and "NoName" respectively.
3. It has a parameterized constructor that allows initialization of radius and name.
4. It includes mutator methods setRadius(double radius) and setName(String name) to modify the
values of radius and name respectively.
5. It provides accessor methods getRadius() and getName() to retrieve the values of radius and
name
6. It has a method sphereVolume() that computes and returns the volume of the sphere based on
the given formula Volume = (3/4) * π * r^3

3. Write a main class SphereTest that:


a. Instantiate class Sphere with object name s1. (Use the default constructor)
b. Call the appropriate mutator methods to set the values of radius and name of s1 with 3.1 for the
radius and name sphere1.
c. Display the volume of the sphere s1.
d. Display the information of sphere s1.

In this Java class SphereTest, the specified requirements are fulfilled:

1. It instantiates the Sphere class with an object named s1 using the default constructor.
2. It calls the appropriate mutator methods setRadius(double radius) and setName(String name) to set
the values of radius and name for s1 with 3.1 and "sphere1" respectively
3. It calculates and displays the volume of the sphere s1 using the sphereVolume() method.
4. It displays the information of sphere s1 by retrieving the radius and name using accessor methods
getRadius() and getName()

You might also like