You are on page 1of 3

CCC 101N: Computer Programming 1

Laboratory Activity: Methods

GUIDANCE:

File location
Place all your programs in the Desktop folder for easy access.
Link
Link the Java compiler to the directory where your programs reside. In our case the programs are in the
Desktop folder.
a. Open Command Prompt.

Key +R on the keyboard and type cmd.

b. Navigate to the directory where your programs are placed. In our case we key this DOS command
through the keyboard in the Command Prompt

cd Desktop

It should take us to the Desktop folder.

c. Key this through the keyboard to link the java compiler to the directory

set path = “C:\Program Files\Java\jdk1.7.0_45\bin”

(Important notes: The JDK’s folder name depends on the JDK version. In our case it is “jdk1.7.0_45”. If you
are using a 32 bit version of JDK, it is located in “Program Files (x86)”.)

Compile
To compile the program, key this command in the Command Prompt
javac NameOfOurProgram.java

Running the Program


To test the program, key this command in the Command Prompt
java NameOfOurProgram

For Keyboard Inputs


Java has Scanner and BufferedReader class libraries. These classes are commonly used for console
inputs. They are similar to “cin >>” and “scanf()” of C and C++. In our programming course, we are going to
use the Scanner class.

To use this class, import the library to your program by keying this
import java.util.Scanner;

then instantiate a Scanner object by keying this


Scanner nameOfScanner = new Scanner(System.in);

(Important note: You can change the identifier “nameOfScanner” name to your preferred name.)

nameOfScanner.nextInt() for integer inputs


nameOfScanner.nextLine() or nameOfScanner.next() for String inputs
nameOfScanner.nextDouble() for double inputs

Just search for the complete list of Scanner input methods in the Internet.

For Output or Display


Key this System.out.print(); or System.out.println();
Ex.
int num = 5;
System.out.print(“The number is ” + num);
Default Java Code Environment

/**
* @file Name of the Program (.java)
* @description Provide a description of the program/file
* (what is this file supposed to do)
* @course CCC 101 Section W
* @lab Lab Activity 3
* @date 09/20/2018
* @author YOUR SURNAME, Your First Name
*/
import java.util.Scanner;

public class NameOfYourProgram


{
public static void main(String[] args)
{
Scanner nameOfYourScanner = new Scanner(System.in);

//This is where you write your code.


//…
//…
}

Laboratory Activity 3.1

Sum of Number Digits Program

Write a method and name it as addDigits. This method takes utmost five (5) digit integer parameter, say num.
This method adds all digits of an integer and returns the sum.

For example, if the integer is 25478 then 2 + 5 + 4 + 7 + 8 returns 26.

Write the complete program to test your method. Name your program as SumOfNumberDigitsProgram. Your
program should look like the Sample Dialogs below. Don’t forget to include the header part.

Sample Dialog 1:

This is Sum of Number Digits Program.

Enter an integer: 25478

The sum of the digits of the integer is 26.

Sample Dialog 2:

This is Sum of Number Digits Program.

Enter an integer: 319

The sum of the digits of the integer is 13.

Laboratory Activity 3.2

Check Binary Number Program

Write a method and name it as isBinaryNumber. This method takes utmost five (5) digit integer parameter, say
num. This method then checks the number if it is a binary number or not. It returns a boolean value – true when the
number is a binary number and false otherwise.
For example, 1101 returns true.
1021 returns false (since a binary number can only be composed of 1 and/or 0 digits).

Write the complete program to test your method. Name your program as CheckBinaryNumberProgram. Your
program should look like the Sample Dialogs below. Don’t forget to include the header part.

Sample Dialog 1:

This is Check Binary Number Program.

Enter a number: 1101

1101 is a binary number.

Sample Dialog 2:

This is Check Binary Number Program.

Enter a number: 301

301 is a NOT binary number.

What to submit:

1. The SumOfNumberDigitsProgram.java of Laboratory Activity 3.1 and


2. The CheckBinaryNumberProgram.java of Laboratory Activity 3.2

You might also like