How many natural numbers less than 300 are neither multiples of 2 or multiples of 3

You need to do these exercises by yourself. Please don't ask me for solutions!

Getting Started Exercises

HelloWorld

  1. Install JDK on your machine. Follow the instructions in "How to Install JDK".
  2. Write a Hello-world program using JDK and a source-code editor, such as:
    • For All Platforms: Sublime Text, Atom
    • For Windows: TextPad, NotePad++
    • For macOS: jEdit, gedit
    • For Ubuntu: gedit
  3. Read "Introduction to Java Programming for Novices & First-Time Programmers". Do ALL the exercises.

CheckPassFail [if-else]

Write a program called CheckPassFail which prints "PASS" if the int variable "mark" is more than or equal to 50; or prints "FAIL" otherwise. The program shall always print “DONE” before exiting.

Hints

Use >= for greater than or equal to comparison.

public class CheckPassFail {  
   public static void main[String[] args] {  
      int mark = 49;   
      System.out.println["The mark is " + mark];
 
      
      if [ ...... ] {
         System.out.println[ ...... ];
      } else {
         System.out.println[ ...... ];
      }
      System.out.println[ ...... ];
   }
}

Try mark = 0, 49, 50, 51, 100 and verify your results.

Take note of the source-code indentation!!! Whenever you open a block with '{', indent all the statements inside the block by 3 [or 4 spaces]. When the block ends, un-indent the closing '}' to align with the opening statement.

CheckOddEven [if-else]

Write a program called CheckOddEven which prints "Odd Number" if the int variable “number” is odd, or “Even Number” otherwise. The program shall always print “bye!” before exiting.

Hints

n is an even number if [n % 2] is 0; otherwise, it is an odd number. Use == for comparison, e.g., [n % 2] == 0.

public class CheckOddEven {   
   public static void main[String[] args] {  
      int number = 49;       
      System.out.println["The number is " + number];
      if [ ...... ] {
         System.out.println[ ...... ];
      } else {
         System.out.println[ ...... ];
      }
      System.out.println[ ...... ];
   }
}

Try number = 0, 1, 88, 99, -1, -2 and verify your results.

Again, take note of the source-code identation! Make it a good habit to ident your code properly, for ease of reading your program.

PrintNumberInWord [nested-if, switch-case]

Write a program called PrintNumberInWord which prints "ONE", "TWO",... , "NINE", "OTHER" if the int variable "number" is 1, 2,... , 9, or other, respectively. Use [a] a "nested-if" statement; [b] a "switch-case-default" statement.

Hints
public class PrintNumberInWord {   
   public static void main[String[] args] {
      int number = 5;  
 
      
      if [number == 1] {   
         System.out.println[ ...... ];
      } else if [ ...... ] {
         ......
      } else if [ ...... ] {
         ......
      ......
      ......
      } else {
         ......
      }
 
      
      switch[number] {
         case 1: 
            System.out.println[ ...... ]; break;  
         case 2: 
            System.out.println[ ...... ]; break;
         ......
         ......
         default: System.out.println[ ...... ]; 
      }
   }
}

Try number = 0, 1, 2, 3, ..., 9, 10 and verify your results.

PrintDayInWord [nested-if, switch-case]

Write a program called PrintDayInWord which prints “Sunday”, “Monday”, ... “Saturday” if the int variable "dayNumber" is 0, 1, ..., 6, respectively.  Otherwise, it shall print "Not a valid day". Use [a] a "nested-if" statement; [b] a "switch-case-default" statement.

Try dayNumber = 0, 1, 2, 3, 4, 5, 6, 7 and verify your results.

Exercises on Number Systems [for Science/Engineering Students]

To be proficient in programming, you need to be able to operate on these number systems:

  1. Decimal [used by human beings for input and output]
  2. Binary [used by computer for storage and processing]
  3. Hexadecimal [shorthand or compact form for binary]

Read "Number Systems" section of "Data Representation", and complete the exercises.

Writing Good Programs

The only way to learn programming is program, program and program. Learning programming is like learning cycling, swimming or any other sports. You can't learn by watching or reading books. Start to program immediately. On the other hands, to improve your programming, you need to read many books and study how the masters program.

It is easy to write programs that work. It is much harder to write programs that not only work but also easy to maintain and understood by others – I call these good programs. In the real world, writing program is not meaningful. You have to write good programs, so that others can understand and maintain your programs.

Pay particular attention to:

  1. Coding Style:
    • Read "Java Code Convention" [@ //www.oracle.com/technetwork/java/codeconventions-150003.pdf or google "Java Code Convention"].
    • Follow the Java Naming Conventions for variables, methods, and classes STRICTLY. Use CamelCase for names. Variable and method names begin with lowercase, while class names begin with uppercase. Use nouns for variables [e.g., radius] and class names [e.g., Circle]. Use verbs for methods [e.g., getArea[], isEmpty[]].
    • Use Meaningful Names: Do not use names like a, b, c, d, x, x1, x2, and x1688 - they are meaningless. Avoid single-alphabet names like i, j, k. They are easy to type, but usually meaningless. Use single-alphabet names only when their meaning is clear, e.g., x, y, z for co-ordinates and i for array index. Use meaningful names like row and col [instead of x and y, i and j, x1 and x2], numStudents [not n], maxGrade, size [not n], and upperbound [not n again]. Differentiate between singular and plural nouns [e.g., use books for an array of books, and book for each item].
    • Use consistent indentation and coding style. Many IDEs [such as Eclipse/NetBeans] can re-format your source codes with a single click.
  2. Program Documentation: Comment! Comment! and more Comment to explain your code to other people and to yourself three days later.

Exercises on Decision and Loop

SumAverageRunningInt [Decision & Loop]

Write a program called SumAverageRunningInt to produce the sum of 1, 2, 3, ..., to 100. Store 1 and 100 in variables lowerbound and upperbound, so that we can change their values easily. Also compute and display the average. The output shall look like:

The sum of 1 to 100 is 5050
The average is 50.5
Hints
public class SumAverageRunningInt {   
   public static void main [String[] args] {
      
      int sum = 0;          
      double average;       
      final int LOWERBOUND = 1;
      final int UPPERBOUND = 100;

            for [int number = LOWERBOUND; number = 'a' && c = '0' && inChar = 'A' && inChar = 'a' && inChar 

Chủ Đề