Download lab 8

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Lab8
Defining Classes ||
Outline and Objectives
Objectives:


The student should be able to solve programming problems with OOP concepts .
The student should be able to read and write a java code with static methods and variables.
The student should be able to read and write a java code Math class .
Introduction:STATIC METHOD
// Method that does not use the instance variables
public static double square (double a)
{
return (a * a);
}
The exit method of the System class:
System.exit(0);
//All the Methods of the Math class are static
The max method of the Math class:
Math.max(-9,12);
STATIC Variables
// a variable that is available among all objects/instances
private static int round = 0;
The PI constant (static & final) of the Math class:
Math.PI;
NOTE: There is no need to import Math class because it is already in lang package .
Exercise 1:1-What output will be produced by the following program?
class MathDemo{
public static void main (String[] args){
System.out.println(Math.max(-10,-14));
System.out.println(Math.min(-8,-4));
System.out.println(Math.ceil(8.4));
System.out.println(Math.floor(8.4));
System.out.println(Math.pow(2, 4));
System.out.println(Math.sqrt(25));
System.out.println(Math.abs(-84));
}}
Exercise 2:- (Languages.java),( LanguagesTest.java)
Correct the following java code. Highlight the errors and identify the reasons .Try to solve it on paper
before using JCreator .
class Languages {
String title ;
static void display() {
System.out.println("Java is my favorite programming language ");
System.out.println(title +" is my favorite programming language ");
show();}
private void show(){
System.out.println("Java is OOP Language.");}
}
class LanguagesTest {
public static void main(String[] args) {
Languages l = new Languages();
title = "Java";
l.display();
l.show();
}
}
Exercise 3:- (LanguagesTwo.java)
Retype or copy the following java code on JCreator . Discuss the results with your lab instructor .
class LanguagesTwo{
static void display() {
System.out.println("Java is my favorite programming language");}
void show(){
System.out.println("Java is OOP Language.");}
public static void main(String[] args) {
display();
show();
LanguagesTwo l = new
LanguagesTwo();
l.display();
}
}
Exercise 4:- (MyZakkat.java)
Design then implement a Java class called MyZakkat. it has a totalMoneyAmount, as double .
Parameterized constructors take one arguments totalMoneyAmount to initialize totalMoneyAmount
andMyZakkat.
.
The class should also have
 Accessor method “gettotalMoneyAmount ()” to return the totalMoneyAmount value.
 a static method calMyZakkat without arguments to calculate amount of my zakkat, then returns
my zakkat after applying the formula zakkat = totalMoneyAmount / 40;
Exercise 5:- (MyZakkatTest.java)
Write a test class named MyZakkatTest for the MyZakkat class above with the main method that tests
MyZakkat class and Create one MyZakkat object then test all above methods.
University of Hail, Girls Branch
Course: ICS102:- Computer Programming
Second semester 2013-2014
Prepared by:-Salma AL-Mouzini