Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

CCC 101N: Computer Programming 1

Laboratory Activity: Programming Basics

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 1
* @date 09/06/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 1.1, you may encode the code segments and try running the programs to get the right answers.

Laboratory Activity 1.1

What is the output of the following code segments?

a.

int r = 0;
int num = 3;
result = (num%2!=1) ? ++num : num++;
System.out.println( r );

b.

int a = 6;

int b = 2;

int c = 0;

boolean isTrue = true;

c = b + a++;

isTrue = (a > 6) | (--b >= 0);

System.out.println(a);

System.out.println(b);

System.out.println(“c”);

System.out.println(isTrue);
c.

boolean x = false;

boolean y = true;

boolean r = ((!x) != (y)) ^ (!(y == x));

System.out.println(y ^ x);

System.out.println(r);

d.

String result=“”;

result = (!(!((true & false) || ((!true && true) ^ (true != false)))))? “Avail!” : “Sorry!”;

System.out.println(result);

Laboratory Activity 1.2

Encode and run this program to determine the output. Study and understand the behavior of this program in
preparation for the next activity. Save this program as “PerimeterAndAreaProgram.java”. Do not forget to write the
header part at the top of your source code.

import java.util.Scanner;

public class PerimeterAndAreaProgram


{

public static void main(String[] args)


{
Scanner scan = new Scanner(System.in);
double PI = 3.14159;
int radius = 0;
int length = 0;
int width = 0;
double perimeterOfCircle = 0;
double areaOfCircle = 0;
double perimeterOfRectangle = 0;
double areaOfRectangle = 0;

System.out.print("Please enter radius of a circle: ");


radius = scan.nextInt();

/*
* Perimeter of a circle is
* 2 * pi * r
* where r is a radius of a circle.
*/
perimeterOfCircle = 2 * PI * radius;

/*
* Area of a circle is
* pi * r * r
* where r is a radius of a circle.
*/
areaOfCircle = PI * radius * radius;

length = scan.nextInt();

System.out.print("Please enter width of a rectangle: ");


width = scan.nextInt();
System.out.println("\nPerimeter of a circle is " + perimeterOfCircle);
System.out.println("Area of a circle is " + areaOfCircle + "\n");

System.out.print("Please enter length of a rectangle:");


length = scan.nextInt();

System.out.print("Please enter width of a rectangle: ");


width = scan.nextInt();

/*
* Perimeter of a rectangle is
* 2 * (length + width)
*/
perimeterOfRectangle = 2 * (length + width);

System.out.println("\nPerimeter of a rectangle is " + perimeterOfRectangle);

/*
* Area of a rectangle is
* length * width
*/
areaOfRectangle = length * width;

System.out.println("Area of a rectangle is " + areaOfRectangle);


}

}
Laboratory Activity 1.3

Simple Calculator program

Write a simple calculator program that accepts two numbers from the keyboard, performs the addition operation,
subtraction operation, multiplication operation, division operation and modulo operation, and prints the results – that is,
the sum, difference product and quotient and remainder of the two numbers. Name this program as
SimpleCalculatorProgram. Don’t forget to include the header part.

Sample Dialog

This is a Simple Calculator Program.

Enter the first number: 6


Enter the second number: 3

The sum of 6 and 3 is 9.


The difference of 6 and 3 is 3.
The product of 6 and 3 is 18.
The quotient of 6 and 3 is 2 and the remainder is 0.

You might also like