User:Janabsalon

Literals Integers binary (introduced in Java SE 7) 0b11110101 (0b followed by a binary number) octal 0365 (0 followed by an octal number) hexadecimal 0xF5 (0x f

Literals Integers binary (introduced in Java SE 7) 0b11110101 (0b followed by a binary number) octal 0365 (0 followed by an octal number) hexadecimal 0xF5 (0x followed by a hexadecimal number) decimal 245 (decimal number) Floating-point values float 23.5F, .5f, 1.72E3F (decimal fraction with an optional exponent indicator, followed by F) 0x.5FP0F, 0x.5P-6f (0x followed by a hexadecimal fraction with a mandatory exponent indicator and a suffix F) double 23.5D, .5, 1.72E3D (decimal fraction with an optional exponent indicator, followed by optional D) 0x.5FP0, 0x.5P-6D (0x followed by a hexadecimal fraction with a mandatory exponent indicator and an optional suffix D) Character literals char 'a', 'Z', '\u0231' (character or a character escape, enclosed in single quotes) Boolean literals boolean true, false null literal null reference null String literals String "Hello, World" (sequence of characters and character escapes enclosed in double quotes) Characters escapes in strings Unicode character \u3876 (\u followed by the hexadecimal unicode code point up to U+FFFF) Octal escape \352 (octal number not exceeding 377, preceded by backslash) Line feed \n Carriage return \r Form feed \f Backslash \\ Single quote \' Double quote \" Tab \t Backspace \b Integer literals are of int type by default unless long type is specified by appending L or l suffix to the literal, e.g. 367L. Since Java SE 7 it is possible to include underscores between numbers to increase readability, for example a number 145608987 may be written as 145_608_987.

Variables[edit] Variables are identifiers associated with values. They are declared by writing the variable's type and name, and are optionally initialized in the same statement by assigning a value.

int count; //Declaring an uninitialized variable called 'count', of type 'int' count = 35; //Initializing the variable int count = 35; //Declaring and initializing the variable at the same time Multiple variables of the same type can be declared and initialized in one statement using comma as a delimiter.

int a, b; //Declaring multiple variables of the same type int a = 2, b = 3; //Declaring and initializing multiple variables of the same type Code blocks[edit] The separators { and } are used to signify a code block and a new scope. Class members and the body of a method are examples of what can live inside these braces in various contexts.

Inside of method bodies you can use the braces to create new scopes like so:

void doSomething() {

   int a;

   {
       int b;
       a = 1;
   }

   a = 2;
   b = 3; // Illegal because the variable is declared in an inner scope.

} //////////////////////////////////////////////////////////// //////////////////////////////////////////////////////// ////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////// Declare your Random variable as a member of the class, not inside any method. Initialize and seed your random variable in your Constructor (What does this mean exactly?) Write your Coin.java one method at a time and test each method independently.


Here's what I have at the moment:

Coin.java

view sourceprint? 01

   public class Coin {

02

03

   public static final int HEADS = 0;

04

   public static final int TAILS = 1;

05

   private int face;

06

   public Coin () { // constructor

07

   flip();

08

   }

09

   public void flip (){ // method “procedure”

10

       face = (int) (Math.random() * 2);

11

   }

12

   public int getFace (){ // method “function”

13

       return face;

14

   }

15

   public String toString(){ // method “function”

16

       String faceName;

17

       if (face == HEADS)

18

           faceName = "Heads";

19

       else

20

           faceName = "Tails";

21

       return faceName;

22

   }

23 } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /* 02

   CoinTester.java - tests the Coin class by

03

   constructing variables and calling it's methods

04

  • /

05 public class CoinTester 06 { 07

 public static void main( String args[] )

08

 {

09

   Coin coin1 = new Coin();

10

   Coin coin2 = new Coin();

11

12

   // FLIP COIN1, PRINT RESULTS

13

14

   System.out.println("\nFlipping Coin1 20 times.");

15

   for (int i=0 ; i<20 ; ++i)

16

       System.out.print( coin1.flip() + " " ); // Equal chance of head or tail

17

   System.out.println();

18

19

   System.out.println("heads=" + coin1.getNumHeads() +

20

            ", tails=" + coin1.getNumTails() );

21

   coin1.reset(); // sets numHeads and numTails back to zero;

22

23

   // FLIP COIN2, PRINT RESULTS

24

25

   System.out.println("\nFlipping Coin2 10 times.");

26

   for (int i=0 ; i<10 ; ++i)

27

       System.out.print( coin2.flip() + " " ); // Equal chance of head or tail

28

   System.out.println();

29

30

   System.out.println("heads=" + coin2.getNumHeads() +

31

           ", tails=" + coin2.getNumTails() );

32

   coin2.reset(); // sets numHeads and numTails back to zero;

33

34

   // FLIP COIN2 AGAIN, PRINT RESULTS

35

36

   System.out.println("\nFlipping Coin1 again 35 times.");

37

   for (int i=0 ; i<35 ; ++i)

38

      System.out.print( coin1.flip() + " " ); // Equal chance of head or tail

39

   System.out.println();

40

41

   System.out.println("heads=" + coin1.getNumHeads() +

42

           ", tails=" + coin1.getNumTails() );

43

   coin1.reset(); // sets numHeads and numTails back to zero;

44

 }// END main

45 }//EOF

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.