33
لعام فيرجىلنفع اات مجانية ل النوتلمساهمة اغ عنب باي خطأ أ أوحظات مها ضرورية ترا برسالة نصية062 4444 9 أولكترونيلبريد ا باPhysics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy م. حمادة شعبان260 4444 9 hs.com - eng info@ ولة محل شرح ومسائل مجانا بالموقعينnet hs. - eng , com hs. - eng Chapter 6 Methods: A Deeper Look

Methods: A Deeper Look - موقع المهندس ... · You write Java programs by combining new methods and ... it returns either a result or simply control ... - The keyword static

Embed Size (px)

Citation preview

بالبريد اإللكترونيأو 9 4444 062 برسالة نصيةتراها ضرورية مالحظات أوأي خطأ باإلبالغ عن المساهمةالنوتات مجانية للنفع العام فيرجى

Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy

neths.-eng, comhs.-eng بالموقعين مجانا شرح ومسائل محلولة @hs.com-enginfo 9 4444 260 حمادة شعبان. م

May 2014

Chapter 6

Methods: A Deeper Look

بالبريد اإللكترونيأو 9 4444 062 برسالة نصيةتراها ضرورية مالحظات أوأي خطأ باإلبالغ عن المساهمةالنوتات مجانية للنفع العام فيرجى

Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy

neths.-eng, comhs.-eng بالموقعين مجانا شرح ومسائل محلولة @hs.com-enginfo 9 4444 260 حمادة شعبان. م

May 2014

6.1 Introduction

The best way to develop and maintain a large program is to construct it from

small, simple pieces, or modules. This technique is called divide and conquer.

It is possible to call certain methods, called static methods, without the need

for an object of the class to exist.

6.2 Program Modules in Java

You write Java programs by combining new methods and classes with

predefined ones available in the API. When possible, reuse Java API classes

and methods. This reduces program development time and avoids new

errors.

The reason we should modularize:

1- It provides the divide and conquer approach.

2- It allows us to use existing methods as building blocks to create new

programs.

3- It avoids repeating codes.

بالبريد اإللكترونيأو 9 4444 062 برسالة نصيةتراها ضرورية مالحظات أوأي خطأ باإلبالغ عن المساهمةالنوتات مجانية للنفع العام فيرجى

Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy

neths.-eng, comhs.-eng بالموقعين مجانا شرح ومسائل محلولة @hs.com-enginfo 9 4444 260 حمادة شعبان. م

May 2014

Note:every method should be limited to perform a single task. This is easier

to test and debug.

A method is invoked by a method call, and when the called method

completes its task, it returns either a result or simply control to the caller.

The caller asks the called method to perform a task and report back the

results. The caller does not know how the called method performs its task

which can be done by calling other methods as well.

6.3 static Methods, static Fields and Class Math

Although most methods execute in response to method calls on specific

objects, sometimes a method performs a task that does not depend on the

contents of any object. Such a method is called a static method or a class

method. For example, we used static method pow of class Math.

For any class imported into your program, you can call its static methods by

ClassName.methodName( arguments )

For example, you can calculate the square root of 900.0 by

Math.sqrt(900.0)

without the need to create a Math object before calling sqrt method.

All Math class methods are static.

بالبريد اإللكترونيأو 9 4444 062 برسالة نصيةتراها ضرورية مالحظات أوأي خطأ باإلبالغ عن المساهمةالنوتات مجانية للنفع العام فيرجى

Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy

neths.-eng, comhs.-eng بالموقعين مجانا شرح ومسائل محلولة @hs.com-enginfo 9 4444 260 حمادة شعبان. م

May 2014

Next figure summarizes several Math class methods where x and y are

double.

بالبريد اإللكترونيأو 9 4444 062 برسالة نصيةتراها ضرورية مالحظات أوأي خطأ باإلبالغ عن المساهمةالنوتات مجانية للنفع العام فيرجى

Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy

neths.-eng, comhs.-eng بالموقعين مجانا شرح ومسائل محلولة @hs.com-enginfo 9 4444 260 حمادة شعبان. م

May 2014

Math Class constants PI and E

Class Math declares two fields that represent commonly used mathematical

constants- Math.PI and Math.E. These fields are declared in class Math with

the modifiers public, final and static.

1- Making them public allows you to use them in your own classes.

2- Any field declared with the word final is a constant, its value can not be

changed after it has been initialized.

3- Making it static allows them to be accessed directly.

Instance variables vs. class variables

Each object of a class has its own copy of the class’s local variables. But there

are fields which each object of the class does not have a separate instance of

the field. That’s the case with static fields – also known as class variables.

When objects of a class containing static fields are created, all the objects of

that class share one copy of the class’s static fields.

Note:Declaring main as static method allows the JVM to invoke main without

creating an instance of the class. We run our program by providing the class

name that contains our main method.

بالبريد اإللكترونيأو 9 4444 062 برسالة نصيةتراها ضرورية مالحظات أوأي خطأ باإلبالغ عن المساهمةالنوتات مجانية للنفع العام فيرجى

Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy

neths.-eng, comhs.-eng بالموقعين مجانا شرح ومسائل محلولة @hs.com-enginfo 9 4444 260 حمادة شعبان. م

May 2014

6.4 Declaring Methods with Multiple Parameters

بالبريد اإللكترونيأو 9 4444 062 برسالة نصيةتراها ضرورية مالحظات أوأي خطأ باإلبالغ عن المساهمةالنوتات مجانية للنفع العام فيرجى

Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy

neths.-eng, comhs.-eng بالموقعين مجانا شرح ومسائل محلولة @hs.com-enginfo 9 4444 260 حمادة شعبان. م

May 2014

- The keyword static in line 28 enables the main method (another static

method) to call maximum without the class name (MaximumFinder).

Static methods in the same class can call each other directly.

- When maximum is called, the parameters x, y and z are initialized with

the values of arguments number1, number2 and number3 respectively.

- There must be one argument in the method call for each parameter in

the method declaration and each argument must be consistent with

the type of the corresponding parameter.

- When program control returns to the point in the program where

maximum was called, maximum’s parameters x, y and z no longer exist

in memory.

- The entire body of our maximum method could be replaced with

returnMath.max( x, Math.max( y, z ) );

- Before any method can be called, its arguments must be evaluated to

determine their value.

- When both operands of operator + are String, operator + creates a new

String with both of these Strings. For example, “hello “+”there” creates

“hello there”. This is called String concatenation.

- The expression “Maximum is: “ + result uses String concatenation.

Every value and object in Java has a String representation. When one of

the operands of + is a String, the other one is converted to its String

representation and then two operands are concatenated.

Note:a boolean concatenated with a String is converted to the String “true”

or “false”. All objects have a toString method that returns a String

representation of the object. The method is implicitly called when an object

is concatenated with a string.

بالبريد اإللكترونيأو 9 4444 062 برسالة نصيةتراها ضرورية مالحظات أوأي خطأ باإلبالغ عن المساهمةالنوتات مجانية للنفع العام فيرجى

Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy

neths.-eng, comhs.-eng بالموقعين مجانا شرح ومسائل محلولة @hs.com-enginfo 9 4444 260 حمادة شعبان. م

May 2014

6.5 Notes on Declaring and Using Methods

There are three ways to call a method:

1. Using a method name by itself to call another method of the same

class.e.g: Maximum(number1, number2, number3);

2. Using a variable that contains a reference to an object followed by a

dot (.) and the method name to call a non-static method.

e.g: myGradeBook.displayMessage();

3. Using the class name and a dot(.) to call a static method of a class.

e.g: Math.sqrt(900.0);

A static method can only call other static methods of the same class directly

and can manipulate only static variables in the same class directly. To access

the class’s non-static members, a static method must use a reference to an

object of the class.

Many objects of a class, each with its own copies of the instance variables,

may exist at the same time. Suppose a static method were to invoke a non-

static method directly. How would the method know which object’s instance

variables to manipulate? That is why Java does not allow a static method to

access non-static members of the same class directly.

Note:redeclaring a parameter as a local variable in the method’s body is a

compilation error.

Note:forgetting to return a value from a method that should return a value is

a compilation error. Returning a value from a method whose return type is

void is also a compilation error.

بالبريد اإللكترونيأو 9 4444 062 برسالة نصيةتراها ضرورية مالحظات أوأي خطأ باإلبالغ عن المساهمةالنوتات مجانية للنفع العام فيرجى

Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy

neths.-eng, comhs.-eng بالموقعين مجانا شرح ومسائل محلولة @hs.com-enginfo 9 4444 260 حمادة شعبان. م

May 2014

6.6 Method-Call Stack and Activation Records

When a program calls a method, the called method must know how to return

to its caller, so the return address of calling method is pushed onto the

method-call stack. If a series of method calls occurs, the successive return

addresses are pushed onto the stack in last-in, first-out order so that each

method can return to its caller.

When a method call is made, the local variables for that method call are

pushed onto the stack. When the method returns to its caller, these variables

are popped off the stack and are no longer know to the program. If more

method calls occur than the stack can store, an error known as a stack

overflow occurs.

بالبريد اإللكترونيأو 9 4444 062 برسالة نصيةتراها ضرورية مالحظات أوأي خطأ باإلبالغ عن المساهمةالنوتات مجانية للنفع العام فيرجى

Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy

neths.-eng, comhs.-eng بالموقعين مجانا شرح ومسائل محلولة @hs.com-enginfo 9 4444 260 حمادة شعبان. م

May 2014

6.7 Arguments Promotion and Casting

A program can call Math method sqrt with an int argument even though a

double argument is expected. For example, the statement

System.out.println(Math.sqrt( 4 ) );

correctly evaluates Math.sqrt(4) and prints the value 2.0.

Java converts the int value to the double value 4.0 before passing the value

to method sqrt. Promotion rules specify which conversions are allowed. And

int is converted to a double without changing its value. However, converting

a double to an int truncates the factional part of the double value, and so

part of the value is lost.

بالبريد اإللكترونيأو 9 4444 062 برسالة نصيةتراها ضرورية مالحظات أوأي خطأ باإلبالغ عن المساهمةالنوتات مجانية للنفع العام فيرجى

Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy

neths.-eng, comhs.-eng بالموقعين مجانا شرح ومسائل محلولة @hs.com-enginfo 9 4444 260 حمادة شعبان. م

May 2014

The valid promotions for a given type are always to a type higher. Converting

values to types lower will result in different values if the lower type cannot

represent the value of the higher type. In this case, Java compiler requires

you to use a cast operator to explicitly force the conversion to occur.

Example:

If a method square calculates the square of an integer and thus requires an

int argument. To call square with a double argument named doubleValue, we

would write:

square( (int) doubleValue )

thus, if doubleValue’s value is 4.5, the method receives the value 4.

6.8 Java API Packages

Java contains many predefined classes that are grouped into categories of

related classes called packages. These are known as Java API or Java class

library. A great strength of Java is the Java API’s thousands of classes.

بالبريد اإللكترونيأو 9 4444 062 برسالة نصيةتراها ضرورية مالحظات أوأي خطأ باإلبالغ عن المساهمةالنوتات مجانية للنفع العام فيرجى

Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy

neths.-eng, comhs.-eng بالموقعين مجانا شرح ومسائل محلولة @hs.com-enginfo 9 4444 260 حمادة شعبان. م

May 2014

6.9 Case Study: Random-Number Generation

The element of chance can be introduced in a program via an object of class

Random or via the static method random of class Math.

A new random-number generator object can be created as follows:

Random randomNumbers = new Random();

intrandomValue = randomNumbers.nextInt();

nextInt() method generates a random int value in the range -2,147,483,648

to +2,147,483,647, inclusive. The calculation uses the current time of day

which changes constantly to get the random number such that each

execution of a program yields a different sequence of random values.

For example, for coin tossing, the following statement returns 0 or 1.

IntrandomValue = randomNumbers.nextInt(2);

wherenextInt(2) takes (2) as an argument and returns a value from 0 up to,

but not including 2.

بالبريد اإللكترونيأو 9 4444 062 برسالة نصيةتراها ضرورية مالحظات أوأي خطأ باإلبالغ عن المساهمةالنوتات مجانية للنفع العام فيرجى

Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy

neths.-eng, comhs.-eng بالموقعين مجانا شرح ومسائل محلولة @hs.com-enginfo 9 4444 260 حمادة شعبان. م

May 2014

Develop a program that simulates 20 rolls of a six-sided die and displays the

value of each roll.

- In line 16, the argument 6 represents the number of unique values that

nextInt should produce starting from 0, thus giving us the range 0-5. But

we need the range 1-6, so we shift the range of numbers by adding a one.

- The if statement (lines 21-22) starts a new line of output every five

numbers.

بالبريد اإللكترونيأو 9 4444 062 برسالة نصيةتراها ضرورية مالحظات أوأي خطأ باإلبالغ عن المساهمةالنوتات مجانية للنفع العام فيرجى

Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy

neths.-eng, comhs.-eng بالموقعين مجانا شرح ومسائل محلولة @hs.com-enginfo 9 4444 260 حمادة شعبان. م

May 2014

6.10 Case Study: A game of Chance; Introducing Enumerations

The rules of the game are as follows:

You roll two dice. The sum of the two is calculated. On the first throw, if the

sum is 7 or 11, you win, if the sum is 2, 3 or 12, you lose, if the sum is 4, 5, 6,

8, 9 or 10, that sum becomes your “point”. To win, you must continue rolling

the dice until you roll that same point values. You lose by rolling a sum of 7

before making that value.

بالبريد اإللكترونيأو 9 4444 062 برسالة نصيةتراها ضرورية مالحظات أوأي خطأ باإلبالغ عن المساهمةالنوتات مجانية للنفع العام فيرجى

Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy

neths.-eng, comhs.-eng بالموقعين مجانا شرح ومسائل محلولة @hs.com-enginfo 9 4444 260 حمادة شعبان. م

May 2014

بالبريد اإللكترونيأو 9 4444 062 برسالة نصيةتراها ضرورية مالحظات أوأي خطأ باإلبالغ عن المساهمةالنوتات مجانية للنفع العام فيرجى

Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy

neths.-eng, comhs.-eng بالموقعين مجانا شرح ومسائل محلولة @hs.com-enginfo 9 4444 260 حمادة شعبان. م

May 2014

بالبريد اإللكترونيأو 9 4444 062 برسالة نصيةتراها ضرورية مالحظات أوأي خطأ باإلبالغ عن المساهمةالنوتات مجانية للنفع العام فيرجى

Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy

neths.-eng, comhs.-eng بالموقعين مجانا شرح ومسائل محلولة @hs.com-enginfo 9 4444 260 حمادة شعبان. م

May 2014

- Although lines 71 and 72 look the same, they do not necessarily

produce the same result.

- Variable randomNumbers is not declared in the method. Instead, it is

declared as a private static final variable of the class and initialized in

line 8. This enables us to create one Random object that’s reused in

each call to rollDice. If there were a program that contained multiple

instances of class Craps, they would all share this one Random object.

- Variable myPoint is initialized to 0 to ensure that the application will

compile. If you do not initialize myPoint, the compiler gives an error,

because myPoint is not assigned a value in every case of the switch

statement.

- By contrast, gameStatus is assigned a value in every case of the switch

statement, thus it does not need to be initialized.

- Local variable gameStatus is declared to be of a new type called Status.

Type status is a private member of class Craps. Status is a type called an

enumeration. This type declares a set of constants.

- An enumeration is a special kind of class, as with classes, braces

indicate an enum declaration body. Inside the braces is a list of

enumeration constants each with a unique value.

- It’s a convention to use only uppercase letters in the names of

enumeration constants. This makes them stand out and remind you

that they are not variables.

- Variables of type Status can be assigned only the three constants

declared in line 11 or a compilation error will occur.

- The values that result in a win or loss on the first roll are declared as

public static final int constants in lines 14-18.

بالبريد اإللكترونيأو 9 4444 062 برسالة نصيةتراها ضرورية مالحظات أوأي خطأ باإلبالغ عن المساهمةالنوتات مجانية للنفع العام فيرجى

Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy

neths.-eng, comhs.-eng بالموقعين مجانا شرح ومسائل محلولة @hs.com-enginfo 9 4444 260 حمادة شعبان. م

May 2014

6.11 The scope of Declarations

The scope of a declaration is the portion of the program that can refer to the

declared entity by its name.

The basic scope rules are as follows:

1. The scope of a parameter declaration is the body of the method in

which the declaration appears.

2. The scope of a local-variable declaration is from the point at which the

declaration appears to the end of that block.

3. The scope of a local-variable declaration that appears in the

initialization section of a for statement’s header is the body of the for

statement and the other expressions in the header.

4. A method or field’s scope is the entire body of the class. This enables

non-static methods of a class to use the fields and other methods of the

class.

If a local variable or parameter in a method has the same name as a field of

the class, the field is “hidden” until the block terminates execution.

This is called shadowing.

بالبريد اإللكترونيأو 9 4444 062 برسالة نصيةتراها ضرورية مالحظات أوأي خطأ باإلبالغ عن المساهمةالنوتات مجانية للنفع العام فيرجى

Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy

neths.-eng, comhs.-eng بالموقعين مجانا شرح ومسائل محلولة @hs.com-enginfo 9 4444 260 حمادة شعبان. م

May 2014

بالبريد اإللكترونيأو 9 4444 062 برسالة نصيةتراها ضرورية مالحظات أوأي خطأ باإلبالغ عن المساهمةالنوتات مجانية للنفع العام فيرجى

Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy

neths.-eng, comhs.-eng بالموقعين مجانا شرح ومسائل محلولة @hs.com-enginfo 9 4444 260 حمادة شعبان. م

May 2014

6.12 Method Overloading

Methods of the same name can be declared in the same class, as long as they

have different sets of parameters (determined by the number, types and

order of the parameters). This is called method overloading. When an

overloaded method is called, the compiler selects the appropriate method by

examining the number, types and order of the arguments in the call.

بالبريد اإللكترونيأو 9 4444 062 برسالة نصيةتراها ضرورية مالحظات أوأي خطأ باإلبالغ عن المساهمةالنوتات مجانية للنفع العام فيرجى

Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy

neths.-eng, comhs.-eng بالموقعين مجانا شرح ومسائل محلولة @hs.com-enginfo 9 4444 260 حمادة شعبان. م

May 2014

- We did not specify a precision in either case. By default, floating-point

values are displayed with six digits of precision if its not specified in the

format.

- The compiler distinguishes overloaded methods by their signature.

- Method calls cannot be distinguished by return types. If you

haveoverloaded methods that differ only by their return types and you

call one of the methods in a standalone statement as in:

square( 2 );

the compiler will not be able to determine the version of the method to

call.

- When two methods have the same signature and different return

types, the compiler gives an error.

- Overloaded methods can have different return types if the methods

have different parameter lists.

بالبريد اإللكترونيأو 9 4444 062 برسالة نصيةتراها ضرورية مالحظات أوأي خطأ باإلبالغ عن المساهمةالنوتات مجانية للنفع العام فيرجى

Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy

neths.-eng, comhs.-eng بالموقعين مجانا شرح ومسائل محلولة @hs.com-enginfo 9 4444 260 حمادة شعبان. م

May 2014

Exercise 6.6write a complete Java application to prompt the user for the

double radius of a sphere, and call method sphereVolume to calculate and

display the volume of the sphere. Use the following statement to calculate

the volume:

double volume = ( 4.0 / 3.0 ) * Math.PI * Math.pow( radius, 3 )

importjava.util.Scanner;

public class Sphere

{

public static void main( String[] args )

{

Scanner input = new Scanner( System.in );

System.out.print( "Enter radius of sphere: " );

double radius = input.nextDouble();

System.out.printf( "Volume is %f\n", sphereVolume

( radius ) );

}

public static double sphereVolume( double radius )

{

double volume = ( 4.0 / 3.0 ) * Math.PI * Math.pow

( radius, 3 );

return volume;

}

}

بالبريد اإللكترونيأو 9 4444 062 برسالة نصيةتراها ضرورية مالحظات أوأي خطأ باإلبالغ عن المساهمةالنوتات مجانية للنفع العام فيرجى

Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy

neths.-eng, comhs.-eng بالموقعين مجانا شرح ومسائل محلولة @hs.com-enginfo 9 4444 260 حمادة شعبان. م

May 2014

Exercise 6.10Math.floor can be used to round values to the nearest integer,

for example

y = Math.floor( x + 0.5 );

will round the number x to the nearest integer and assign the result to y.

To round numbers to specific decimal places, use a statement like

y = Math.floor( x * 10 + 0.5 ) / 10;

which rounds x to the tenths position, or

y = Math.floor( x * 100 + 0.5 ) / 100;

which rounds x to the hundredths. Write an application that defines four

methods for rounding a number x in various ways:

a) roundToInteger( number )

b) roundToTenths( number )

c) roundToHundredths( number )

d) roundToThousandths( number )

For each value read, your program should display the original value, the

number rounded to the nearest integer, the number rounded to the

nearest tenth, the number rounded to the nearest hundredth and the

number rounded to the nearest thousandth.

importjava.util.Scanner;

public class Rounding

{

public static void main (String[] args)

{

Scanner input = new Scanner(System.in);

double number;

System.out.printf("Enter a double: ");

number = input.nextDouble();

بالبريد اإللكترونيأو 9 4444 062 برسالة نصيةتراها ضرورية مالحظات أوأي خطأ باإلبالغ عن المساهمةالنوتات مجانية للنفع العام فيرجى

Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy

neths.-eng, comhs.-eng بالموقعين مجانا شرح ومسائل محلولة @hs.com-enginfo 9 4444 260 حمادة شعبان. م

May 2014

System.out.printf("The original value:

%f\n",number);

System.out.printf("Rounded to the nearest integer:

%d\n",roundToInteger(number));

System.out.printf("Rounded to tenths:

%.1f\n",roundToTenths(number));

System.out.printf("Rounded to hundredths:

%.2f\n",roundToHundredths(number));

System.out.printf("Rounded to thousandths:

%.3f\n",roundToThousandths(number));

}

private static introundToInteger (double x)

{

return (int) (Math.floor( x + 0.5 ));

}

private static double roundToTenths (double x)

{

return (Math.floor( x*10 + 0.5 )/10);

}

private static double roundToHundredths (double x)

{

return (Math.floor( x*100 + 0.5 )/100);

}

private static double roundToThousandths (double x)

{

return (Math.floor( x*1000 + 0.5 )/1000);

}

}

بالبريد اإللكترونيأو 9 4444 062 برسالة نصيةتراها ضرورية مالحظات أوأي خطأ باإلبالغ عن المساهمةالنوتات مجانية للنفع العام فيرجى

Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy

neths.-eng, comhs.-eng بالموقعين مجانا شرح ومسائل محلولة @hs.com-enginfo 9 4444 260 حمادة شعبان. م

May 2014

Exercise 6.14 Write a method integerPower(base, exponent) that returns

the value of baseexponent

For example, integerPower(3, 4) calculates 34 (or 3 * 3 * 3 * 3). Assume that

exponent is a positive, nonzero integer and that base is an integer. Use a

for or while statement to control the calculation. Do not use any Math class

methods. Incorporate this method into an application that reads integer

values for base and exponent and performs the calculation with the

integerPower method.

importjava.util.Scanner;

public class Expo

{

public static void main (String[] args)

{

Scanner input = new Scanner(System.in);

int base, exponent;

System.out.println("Enter an integer base and an

integer exponent: ");

base = input.nextInt();

exponent = input.nextInt();

System.out.printf("The result is: %d",

integerPower(base,exponent));

}

private static intintegerPower (int b, int e)

{

int result = 1;

for ( inti = 1; i<= e; i++)

result*= b;

return result;

}

بالبريد اإللكترونيأو 9 4444 062 برسالة نصيةتراها ضرورية مالحظات أوأي خطأ باإلبالغ عن المساهمةالنوتات مجانية للنفع العام فيرجى

Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy

neths.-eng, comhs.-eng بالموقعين مجانا شرح ومسائل محلولة @hs.com-enginfo 9 4444 260 حمادة شعبان. م

May 2014

}

Exercise 6.19Write a method that receives as a first parameter the size of a

solid square and a second parameter of type char called fillCharacter and

forms the square using the char provided as an argument. Thus, if size is 5

and fillCharacter is #, the method should display

#####

#####

#####

#####

#####

Use the following statement (in which input is a Scanner object) to read a

character from the user at the keyboard:

char fill = input.next().charAt( 0 );

importjava.util.Scanner;

public class anyChar

{

public static void main (String[] args)

{

Scanner input = new Scanner(System.in);

int number;

char myChar;

System.out.println("Enter a positive integer: ");

number = input.nextInt();

System.out.println("Enter a symbol: ");

myChar = input.next().charAt(0);

squareOfChar(number,myChar);

}

بالبريد اإللكترونيأو 9 4444 062 برسالة نصيةتراها ضرورية مالحظات أوأي خطأ باإلبالغ عن المساهمةالنوتات مجانية للنفع العام فيرجى

Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy

neths.-eng, comhs.-eng بالموقعين مجانا شرح ومسائل محلولة @hs.com-enginfo 9 4444 260 حمادة شعبان. م

May 2014

private static void squareOfChar ( int x, char c)

{

for ( inti = 1; i<= x; i++){

for ( int j = 1; j <= x; j++)

System.out.printf("%c", c);

System.out.println();

}

}

}

بالبريد اإللكترونيأو 9 4444 062 برسالة نصيةتراها ضرورية مالحظات أوأي خطأ باإلبالغ عن المساهمةالنوتات مجانية للنفع العام فيرجى

Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy

neths.-eng, comhs.-eng بالموقعين مجانا شرح ومسائل محلولة @hs.com-enginfo 9 4444 260 حمادة شعبان. م

May 2014

Exercise 6.22 Implement the following integer methods:

a) Method celsius returns the Celsius equivalent of a Fahrenheit

temperature, using thecalculation

celsius = 5.0 / 9.0 * ( fahrenheit - 32 );

b) Method fahrenheit returns the Fahrenheit equivalent of a Celsius

temperature, usingthe calculation

fahrenheit = 9.0 / 5.0 * celsius + 32;

c) Use the methods from parts (a) and (b) to write an application that

enables the user eitherto enter a Fahrenheit temperature and

displaythe Celsius equivalent or to enter a Celsius temperature and

display the Fahrenheit equivalent.

بالبريد اإللكترونيأو 9 4444 062 برسالة نصيةتراها ضرورية مالحظات أوأي خطأ باإلبالغ عن المساهمةالنوتات مجانية للنفع العام فيرجى

Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy

neths.-eng, comhs.-eng بالموقعين مجانا شرح ومسائل محلولة @hs.com-enginfo 9 4444 260 حمادة شعبان. م

May 2014

بالبريد اإللكترونيأو 9 4444 062 برسالة نصيةتراها ضرورية مالحظات أوأي خطأ باإلبالغ عن المساهمةالنوتات مجانية للنفع العام فيرجى

Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy

neths.-eng, comhs.-eng بالموقعين مجانا شرح ومسائل محلولة @hs.com-enginfo 9 4444 260 حمادة شعبان. م

May 2014

Exercise 6.26 Write a method that takes an integer value and returns the

number with its digits reversed. For example, given the number 7631, the

method should return 1367. Incorporate the method into an application

that reads a value from the user and displays the result.

بالبريد اإللكترونيأو 9 4444 062 برسالة نصيةتراها ضرورية مالحظات أوأي خطأ باإلبالغ عن المساهمةالنوتات مجانية للنفع العام فيرجى

Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy

neths.-eng, comhs.-eng بالموقعين مجانا شرح ومسائل محلولة @hs.com-enginfo 9 4444 260 حمادة شعبان. م

May 2014

بالبريد اإللكترونيأو 9 4444 062 برسالة نصيةتراها ضرورية مالحظات أوأي خطأ باإلبالغ عن المساهمةالنوتات مجانية للنفع العام فيرجى

Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy

neths.-eng, comhs.-eng بالموقعين مجانا شرح ومسائل محلولة @hs.com-enginfo 9 4444 260 حمادة شعبان. م

May 2014

Exercise 6.29 Write an application that simulates coin tossing. Let the

program toss a coin each time the user chooses the “Toss Coin” menu

option. Count the number of times each side of the coin appears. Display

the results. The program should call a separate method flip that takes no

arguments and returns a value from a Coin enum (HEADS and TAILS).

[Note: If the program realistically simulates coin tossing, each side of the

coin should appear approximately half the time.]

بالبريد اإللكترونيأو 9 4444 062 برسالة نصيةتراها ضرورية مالحظات أوأي خطأ باإلبالغ عن المساهمةالنوتات مجانية للنفع العام فيرجى

Physics I/II, English 123, Statics, Dynamics, Strength, Structure I/II, C++, Java, Data, Algorithms, Numerical, Economy

neths.-eng, comhs.-eng بالموقعين مجانا شرح ومسائل محلولة @hs.com-enginfo 9 4444 260 حمادة شعبان. م

May 2014