You are on page 1of 4

CCC 101N: Computer Programming 1

Laboratory Activity: Programming Basics & 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 2
* @date 09/11/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.


//…
//…
}

NOTE: For lab activity 2.1, try running the program, play with the source code and observe its behavior.

Laboratory Activity 2.1 Encode and run this program. Save this program as “TestProgram.java”.
import java.util.Scanner;

public class TestProgram


{

public static void main(String[] args)


{
Scanner in = new Scanner(System.in);
int firstNum = 0;
int secNum = 0;
int thirdNum = 0;
int sum = 0;
double average = 0;
System.out.println("This program computes the sum and the average of the three (3) numbers.");
System.out.print("Enter the first number: ");
firstNum = in.nextInt();
System.out.print("Enter the second number: ");
secNum = in.nextInt();
System.out.print("Enter the third number: ");
thirdNum = in.nextInt();

sum = computeSum(firstNum, secNum, thirdNum);


average = computeAve(firstNum, secNum, thirdNum);

System.out.println("The sum of the three numbers is: " + sum);


System.out.println("The average of the three numbers is: " + average);

public static int computeSum(int num1, int num2, int num3)


{
int sum;

sum = num1 + num2 + num3;


return sum;
}

public static double computeAve(int num1, int num2, int num3)


{
int ave;

ave = (computeSum(num1,num2,num3) / 3);


return ave;
}
}
Laboratory Activity 2.2

Binary Addition Program

Write a method and name it as addBinary. This method takes two integer parameters, say num1 and num2, and
returns the sum of the two binary numbers.

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

Sample Dialog 1:

This is Binary Addition Program.

Enter the first binary number: 100


Enter the second binary number: 10

The sum of 100 and 10 is 110.

Sample Dialog 2:

This is Binary Addition Program.

Enter the first binary number: 10


Enter the second binary number: 101

The sum of 10 and 101 is 111.

Laboratory Activity 2.3

Binary Subtraction Program

Write a method and name it as subtractBinary. This method takes two integer parameters, say num1 and num2,
and returns the difference of the two binary numbers. Note that for this program, the first number should be greater
than the second number.

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

Sample Dialog 1:

This is Binary Subtraction Program.

Enter the first binary number: 100


Enter the second binary number: 10

The difference of 100 and 10 is 10.

Sample Dialog 2:

This is Binary Subtraction Program.

Enter the first binary number: 111


Enter the second binary number: 11

The difference of 111 and 11 is 100.


What to submit in Edmodo:

1. The TestProgram.java of Laboratory Activity 2.1


2. The BinaryAdditionProgram.java of Laboratory Activity 2.2 and
3. The BinarySubtractionProgram.java of Laboratory Activity 2.3

Note: Please TURN IN your programs. DO NOT post NOR send them as a private message. In addition, don’t forget to
write the header part of your Laboratory Activity 2.1’s code. Apply this in the other Lab Activities as well.

Deadline of submission: September 16, 2018, Sunday, until 12midnight

You might also like