110CT: Lecture 4a Portfolio Questions 2006-7

 

In exercise 3.1 (Barnes & Kolling ex 1.26) you explored Java's 8 predefined primitive data types. Also, in previous work you've encountered the mathematical operators '+', '-', '/' and '*'. The exercises here explore some issues when using these operators with various data types. To help with this you'll find it useful to first explore BlueJ's CodePad facility. (The CodePad experiments are not needed for your Portfolio.)

 

  • BlueJ provides a CodePad in its own separate pane. This allows you to try out snippets of code before you put it into a class.

  • If the CodePad pane isn't displayed, select it from menu View. CodePad is only useable when a project is loaded.

  • In the CodePad pane type in

    3+4 <Enter>
    and see the result displayed. Try other examples.

  • It is also possible to run complete Java instructions, which maybe don't return a result. Type

    System.out.println("Hi there"); <Enter>

  • Sometimes you want to enter an instruction, which spans several lines, before it can be evaluated. Use Shift-Enter at the end of all lines before the last. Try

     

    int x = 4;       <Shift-Enter>
     if (x == 3) {   <Shift-Enter>
       System.out.println("Hello there");   <Shift-Enter>
     } else {        <Shift-Enter>
       System.out.println("Bye");    <Shift-Enter>
     }    <Enter>
    
  • Variables, like x, are remembered between inputs so can be re-used. Try

       System.out.println(x);   <Enter> 
    

 

If you need it, there's more information on using CodePad in BlueJ's tutorial (chapter 6).


 

You may want to use Barnes and Kolling Appendix D to help with these questions.

It may help you to be aware that in Java the default data type for integer values (whole numbers) is int; the default data type for floating-point numbers (those with a decimal part) is double.

 

  • 4a.1:

    What is the result of 15/10? 15/10.0? Explain why are these different?

  • 4a.2:

    What is the result of 9/10? Explain

  • 4a.3:

    What is the result of 2/3? 2.0/3.0? Explain why are these different?

  • 4a.4:

    What is the result of 5*2+6? 5*(2+6)? Explain why are these different?

  • 4a.5:

    What is the result of (10/3)*3? (10/3.0)*3? Explain why are these different?

  • 4a.6:

    What is the result of:

    
       boolean wind = false;
       if (!(wind)) {
          System.out.println("okay");
       }
    
    Explain. ('!' means 'not')

  • 4a.7:

    What happens if you run:

    
       byte k = 100;
       k=k+100;
    
    Explain.

 

Index | Previous | Next

 

Last modified: 3 Nov 2006
Webpage: © Lisa Payne, Coventry University, 2006
Exercises and projects: © Barnes and Kolling