File Location Link: CCC 101N: Computer Programming 1 Laboratory Activity: Programming Basics
File Location Link: 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.
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
c. Key this through the keyboard to link the java compiler to the directory
(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
To use this class, import the library to your program by keying this
import java.util.Scanner;
(Important note: You can change the identifier “nameOfScanner” name to your preferred name.)
Just search for the complete list of Scanner input methods in the Internet.
/**
* @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;
NOTE: For lab activity 1.1, you may encode the code segments and try running the programs to get the right answers.
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;
c = b + a++;
System.out.println(a);
System.out.println(b);
System.out.println(“c”);
System.out.println(isTrue);
c.
boolean x = false;
boolean y = true;
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);
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;
/*
* 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();
/*
* Perimeter of a rectangle is
* 2 * (length + width)
*/
perimeterOfRectangle = 2 * (length + width);
/*
* Area of a rectangle is
* length * width
*/
areaOfRectangle = length * width;
}
Laboratory Activity 1.3
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