• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Java căn bản - Chapter7
 

Java căn bản - Chapter7

on

  • 717 views

 

Statistics

Views

Total Views
717
Views on SlideShare
717
Embed Views
0

Actions

Likes
0
Downloads
3
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • A class is instantiable if we can create instances of it. For example, the JFrame class is instantiable. A class is noninstantiable if we cannot create its instances. The Math class is one example of noninstantiable classes. The main classes we have been writing are all noninstantiable. We will learn how to define instantiable classes in this lesson. A constructor is a special method that is used to initialize an instance of a class when it is first created.
  • We already know how to return an object from a method, because we have been returning a string from a method and a string is an object (an instance of the String class). However, String objects are treated much like primitive data. We will explore a little deeper into this topic.
  • The use of the reserved word this is option in this context. Calling a method of a class or accessing a data member of the class can be achieved without the reserved word this. Consider, for example, class One { public int m1( ) { return 10; } public void m2( ) { int num; num = m1();  this is equivalent to num = this .m1(); } }
  • Notice that the difference in the return type alone is not enough to overload the methods. For example, the following declaration is invalid public double convert(int num) { . . . } public float convert(int num) { . . . }
  • Notice that the difference in the return type alone is not enough to overload the methods. For example, the following declaration is invalid public double convert(int num) { . . . } public float convert(int num) { . . . }
  • Without using the reserved word this , we need to define the four constructors as follows (it forces us to repeat the same code): public Fraction ( ) { //creates 0/1 setNumerator ( 0 ) ; setDenominator ( 1 ) ; } public Fraction ( int number ) { //creates number/1 setNumerator ( number ) ; setDenominator ( 1 ) ; } public Fraction ( Fraction frac ) { //copy constructor setNumerator ( frac.getNumerator ()) ; setDenominator ( frac.getDenominator ()) ; } public Fraction ( int num, int denom ) { setNumerator ( num ) ; setDenominator ( denom ) ; }
  • In the command prompt window, we used the commands javac and java to compile and run Java programs, respectively. Similarly, to generate javadoc files, we use the javadoc command. For example, to generate a javadoc file for the Fraction class, we enter javadoc -private Fraction.java We specify the -private option because we want to generate the documentation for all types of methods (so far, we have covered two of these— private and public ). The private option generates the most complete documentation.
  • As a part of the overall plan, we begin by identifying the main tasks for the program. Unlike the overall plan for the previous sample developments, we will use a pseudo code to express the top level logic of the program.
  • The structure of this program is very simple. We will use two standard classes, one for input and output and another for generating random numbers.
  • The second and the third steps correspond to the two major tasks identified in the overall plan.
  • In the first step, we determine a little more detailed control logic than the one stated in the overall plan. For each of the five identified functions, we will define a method: describeRules, generateSecretNumber, playGame, and prompt.
  • Please use your Java IDE to view the source files and run the program.
  • Run the program and verify that the topmost control loop is functioning correctly.
  • In order to verify whether our code is working correctly or not, we need to know what is the secret number. The easiest way to do this is to use a fixed number, such as 45, make the temporary generateRandomNumber to return this fixed number.
  • We implement the playGame and getNextGuess methods in this step.
  • We need to verify the correctness of two methods: playGame and getNextGuess. Try all cases presented here and confirm that you get the expected responses.
  • Notice that we have one temporary statement to output the value of secretNumber. We include it for the testing purpose, i.e., we need to check the numbers generated are valid.
  • The body of the inputBooks method is as follows: while ( isContinue ()) { title = readString ( "Title : " ) ; chargePerDay = readDouble ( "Charge per day: " ) ; maxCharge = readDouble ( "Maximum charge: " ) ; dueDate = readDate ( "Due Date : " ) ; book = createBook ( title, chargePerDay, maxCharge, dueDate ) ; bookTracker.add ( book ) ; }
  • As always, we run the final test by running the program numerous times trying out as many variations as possible. Before testing the generateSecretNumber method as a part of the final program, we will use a separate test driver to generate 1000 (or more) secret numbers and verify that they are valid. class TestRandom { public static void main (String[] args) { int N = 1000, count = 0, number; double X; do { count++; X = Math.random(); number = (int) Math.floor( X * 100 ) + 1; } while ( count < N && 1 < 1 || number > 100 ) { System.out.println("Error: " + number); } else { System.out.println("Okay"); } } }
  • As always, we run the final test by running the program numerous times trying out as many variations as possible. Before testing the generateSecretNumber method as a part of the final program, we will use a separate test driver to generate 1000 (or more) secret numbers and verify that they are valid. class TestRandom { public static void main (String[] args) { int N = 1000, count = 0, number; double X; do { count++; X = Math.random(); number = (int) Math.floor( X * 100 ) + 1; } while ( count < N && 1 < 1 || number > 100 ) { System.out.println("Error: " + number); } else { System.out.println("Okay"); } } }

Java căn bản - Chapter7 Java căn bản - Chapter7 Presentation Transcript