Sie sind auf Seite 1von 12

Compilation of

Exercises in
Fundamentals of
Programming

Submitted by
Perlas, Shanaia F.
Salvador, Christian D.
(SBIT-1B)

Submitted to
Mrs. Lorna Estabillo

October 8, 2018

1|Page
Midterm Period
Exercise #1

1.1 Print Hello World.

public class Perlas_Salvador11 {

public static void main(String[] args) {

// TODO, add your application code


System.out.println("Hello World!");
}
}

1.2 Print Hello World, Hello Earth, Hello Universe.

public class Perlas_Salvador12 {

public static void main(String[] args) {

// TODO, add your application code


System.out.println("Hello World!");
System.out.println("Hello Earth!");
System.out.println("Hello Universe!");
}
}

1.3 Create a program that will print your experiences in your first day of class. 10 lines.

public class Perlas_Salvador13{


public static void main(String[] args) {

System.out.println("Hello World -_-! ");

System.out.println("\nI felt an excitement during the first day of our class.");


System.out.println("Because i will meet new people and i know i will learn a lot of things.");
System.out.println("I also felt discomport when the class started,");
System.out.println("And i am shy to introduce myself to everyone.");
System.out.println("My excitement fades away,");
System.out.println("Because most of my classmates already knew each other,");
System.out.println("And i was like out of place");
System.out.println("Though i am happy because i am already a college student");
System.out.println("Im here to pursue my dreams to have a good life.");
System.out.println("Because Good Life Starts Here in Quezon City Polytechnic University");
}
}

2|Page
Exercise #2

2.1 Create a program that will declare the following variable and print with the respective
value.

public class Perlas_Salvador21{


public static void main(String[] args) {
int chris= 5;
int shan= 10;
String chrishan= “\”QCPU\””;
System.out.println (“Salvador_num= ” + chris);
System.out.println (“Salvador_num= ” + shan);
System.out.println (“Salvador_num= ” + chrishan);
}
}

2.2 Declare 2 integer type variables it’s up to you what values they have. The program
will display the value of 2 variables with the sum and product of the 2 variable.

public class Perlas_Salvador22{


public static void main(String[] args) {
int s=5;
int c=10;
System.out.println (“1st num= ” + s);
System.out.println (“2nd num= ” + (s + s ) );
System.out.println (“sum= ” + (s + s + s) );
System.out.println (“product= ” + (c * s) );
}
}

2.3 Declare 2 float variables it’s up to you what values they have. The program will
display the value of 2 variables with difference and quotient of the 2 variable.
public class Perlas_Salvador23{
public static void main(String[] args) {
float s=50.5f;
float c=10.24f;
System.out.println (“1st num= ” + s);
System.out.println (“2nd num= ” + c);
System.out.println (“Quotient= ” + (s/c) );
System.out.println (“Difference= ” + (s*c) );
}
}

Exercise #3
3.1
public class Perlas_Salvador22{
public static void main(String[] args) {
String var1 = “\” CHRISTIAN” ;
String var2 = “SALVADOR \””;
System.out.println ((var1 + var2 ) + “ is my full name ”);
}
}

3|Page
3.2 Create a program that will allow you to input an integer and print it after “You input: “

import java.util,Scanner;
public class Perlas_Salvador32{
public static void main(String[] args) {
Scanner input_new = new Scanner (System.in);
int num1;
System.out.println(“Input number: ”);
num1 = input_new.nextInt ();
System.out.println(“You Input: ” + num1);
}
}

3.3 Create a program that will accept String input. The program will then display “You
input: “ together with the inputted Strings.
import java.util,Scanner;
public class Perlas_Salvador33{
public static void main(String[] args) {
Scanner input_new = new Scanner (System.in);
String a;
System.out.println(“Input Word: ”);
a = input_new.next ();
System.out.println(“\” You Input: ” + a + (“\””) );
}
}

3.4 Create a program that will accept 2 numbers. The program will then display the 30%
of the sum of 2 numbers.

Import java.util,Scanner;
public class Perlas_Salvador32{
public static void main(String[] args) {
Scanner input_new = new Scanner (System.in);
int c;
int s;
System.out.println(“Enter Two Numbers: ”);
c = input_new.nextInt ();
s = input_new.nextInt ();
System.out.println(“30% of the sum of two numbers: ” + ((c + s)* .30));
}
}

4|Page
Exercise #4

4.1 Create a program that will enter an amount and will determine the number of bills and
coins (50’s, 20’s, 10’s, 5’s, 1’s)
import java.util.Scanner;
public class Perlas_Salvador42{
static Scanner in = new Scanner(System.in);
public static void main (String[]args){

int amount, q = 50, w = 20, e = 10, r = 5, t = 1;


int rem1, rem2, rem3, rem4, rem5;
System.out.print(“Enter amount: “);
amount = in.nextInt();
rem1 = amount % q;
rem2 = rem1 % w;
rem3 = rem2 % e;
rem4 = rem3 % r;
rem5 = rem4 % t;

System.out.println(“ The number of 50\’s: “ + (amount / q));


System.out.println(“ The number of 20\’s: “ + (rem1 / w));
System.out.println(“ The number of 10\’s: “ + (rem2 / e));
System.out.println(“ The number of 5\’s: “ + (rem3 / r));
System.out.println(“ The number of 1\’s: “ + (rem4 / t));

}
}

4.2 Create a program that will prompt the user to enter student name, grades and quiz,
class standing and exam. The program will compute the total grades base on the criteria.
Quiz 40% + Class Standing 10% + Exam 50%. The program will also display
whether the grade is “Passed” or “Failed”
import java.util.Scanner;
public class Perlas_Salvador42{

static Scanner input = new Scanner(System.in);


public static void main(String[] args) {

String name;
String surname;
int quiz, cs, exam, grade;
double quiz1 = .40;
double cs1 = .10;
double exam1 = .50;
int q, c, e;
System.out.print("Enter first name: ");
name = input.next();
System.out.print("Enter last name: ");
surname = input.next();
System.out.print("Quiz: ");
quiz = input.nextInt();
System.out.print("Class Standing: ");
cs = input.nextInt();
System.out.print("Exam: ");
exam = input.nextInt();
System.out.print(name + " " + surname );
System.out.print("‘s grade is");

System.out.print(", Passed ");


}
}

5|Page
Exercise #5

5.1 Create a program that will compute the area of the following:
Codes:
Circle = Pi Radius ² ‘c’ or ‘C’
Rectangle = Length * Width ‘r’ or ‘R’
Triangle = ½bh ‘t’ or ‘T’
Square= Side ² ‘s’ or ‘S’

import java.util.Scanner;
public class Perlas_Salvador51{

public static void main (String[]args){

char code;
int radius, length, width, base, height, side;

Scanner in = new Scanner(System.in);

System.out.print("Enter Code: ");


code=in.next().charAt (0);

switch(code) {
case 'c':
System.out.print("\tEnter radius: ");
radius=in.nextInt();
System.out.print("\tThe area of circle is: " + (3.14 * radius*radius));
break;

case 'C':
System.out.print("\tEnter radius: ");
radius=in.nextInt();
System.out.print("\tThe area of circle is: " + (3.14 * radius*radius));
break;

case 'r':
System.out.print("\tEnter length: ");
length=in.nextInt();
System.out.print("\tEnter width: ");
width=in.nextInt();
System.out.print("\tThe area of Rectangle is: " + (length*width));
break;

case 'R':
System.out.print("\tEnter length: ");
length=in.nextInt();
System.out.print("\tEnter width: ");
width=in.nextInt();
System.out.print("\tThe area of Rectangle is: " + (length*width));
break;

case 't':
System.out.print("\tEnter base: ");
base=in.nextInt();
System.out.print("\tEnter height: ");
height=in.nextInt();
System.out.print("\tThe area of Triangle is: " + (base*height)/2);
break;

case 'T':
System.out.print("\tEnter base: ");
base=in.nextInt();

6|Page
System.out.print("\tEnter height: ");
height=in.nextInt();
System.out.print("\tThe area of Triangle is: " + (base*height)/2);
break;

case 's':
System.out.print("\tEnter side: ");
side=in.nextInt();
System.out.print("\tThe area of Square is: " + (side*side));
break;

case 'S':
System.out.print("\tEnter side: ");
side=in.nextInt();
System.out.print("\tThe area of Square is: " + (side*side));
break;
default:
System.out.print("Invalid Entry");
break;

}
}
}

5.2 Create a program that will read a salesman name and sales. Compute for the salary.
Salary=Sales + Commission. (Nested if)

import java.util.Scanner;
public class Perlas_Salvador52{

Scanner in= new Scanner(System.in);


public static void main (String[]args){

String name;
int sales;
double com, salary;

System.out.print(“Please enter salesman’s name: “);


name=in.nextLine();

System.out.print(“Please enter sales: “);


sales=in.nextLine();

if (sales >= 10000){


com=sales*.10;
salary=sales + com;
System.out.print(“The salary of “ + name + “is: ” +salary);
}
else if (sales < 10000 && sales >= 5000)
{
com=sales*.5;
salary=sales + com;
System.out.print(“The salary of “ + name + “is: ” + salary);
7|Page
}

else if (sales >= 10000)


{
com=sales*.02;
salary=sales + com;
System.out.print(“The salary of “ + name + “is: ” +salary);
}
else
System.out.print (“INVALID INPUT!! Please try again”);
}
}

Final Period
Final Exercise #1

1.1 A program that will read your name and age. Print the name and age 6 times if age is
less than 25, otherwise print only the name. (if else and do while)

import java.util.Scanner;
public class Perlas_Salvador_FE11{

static Scanner in= new Scanner(System.in);


public static void main (String[]args){

int key, age;


String name;

do{
System.out.print("Enter name: ");
name=in.next();

System.out.print("Enter age: ");


age=in.nextInt();

if (age< 25)
for(int x=1; x<=6; ++x)
System.out.println( + x + " " + name + ", " + age);
else {
System.out.println(name + ", " + age);
}
System.out.println("Press key 1 to enter another record: ");
key=in.nextInt();
}
while (key ==1);
System.out.print("Program Terminated! ");

}
}

1.2 A Program that will print numbers from 1-10 using for loop, numbers from 20-1 using
while loop and numbers from 3-60 divisible by 3 using do while.

8|Page
import java.util.Scanner;
public class Perlas_SalvadorFE12{

static Scanner shai= new Scanner(System.in);


public static void main(String[]args){
int a;
for (a=1; a<=10; a=a+1)
System.out.println("Numbers: " + a);

String key;
System.out.println("\nPress any key to continue…");
key=shai.next();

{
int b = 20;
while (b >= 1) {
System.out.println("Number: " + b);
b--;
}

System.out.println("\nPress any key to continue…");


key=shai.next();
}

{
int c=1;
do {
System.out.println("Number: " + (c*3));
c++;
}
while (c<=20);
}
System.out.println("\nEnd of Program\n: " );
}
}

Final Exercise #2

Complete the program and perform the needed specification.


•FamilyInfo() – write 5 lines and give details about your family.
•ComputeAge() – a method with an integer variable. It will adds 5 to age if the entered age
is greater than 18
•PrintInfos() – write 3 lines to print your name, subject, entered age and new age.

import java.util.Scanner;
public class Perlas_SalvadorFE2 {

public static void FamilyInfo() {


System.out.println("My father's name is Zerg C. Perlas");
System.out.println("My mother's name is Vanessa F. Perlas");
System.out.println("I have six siblings");
System.out.println("Five girls and one boy");
System.out.println("I am blessed to have them");
}
public static int computeAge(int num){
int result;
if(num > 18)
result = num + 5;
else
result = num;

9|Page
return result;
}
public static void PrintInfo(int newAge){

System.out.println("\nShanaia F. Perlas ");

System.out.println("Fundamentals of Programming");

System.out.println("New age is: " + newAge);

}
public static void main (String[]args) {
int age;
int newAge;
char key;

try {
do{

Scanner input = new Scanner (System.in);


System.out.print("Please enter age: ");
age = input.nextInt();
FamilyInfo();
newAge = computeAge(age);
PrintInfo(newAge);
System.out.print("Do you Want to Continue? (Y/N):");
key = input.next () .charAt (0);

}
while ((key == 'y') || (key == 'y'));
System.out.println("Program Complete");

catch(Exception e){
System.out.println("Wrong input!!!" );

}
}
}

Final Exercise #3

3.1 Declare an array named months and print its elements. It has twelve elements.

public class Perlas_SalvadorFE31 {

public static void main(String[] args) {

String[]months={" ", "January", "February", "March", "April", "May", "June", "July",


"August", "September", "October", "November", "December"};

for(int i=1; i<months.length; ++i)


{

10 | P a g e
System.out.print("Num["+ i + "]: " + months[i] + "\n");
}
System.out.print("\nPROJECT TERMINATED!!");

}
}

3.2 Create a program that will allow you to enter 3 tuition fees and print those three
together.
import java.util.Scanner;
public class Perlas_Salvador {

static Scanner in= new Scanner(System.in);


public static void main(String[] args) {

int [] tf= new int[6];


for(int i= 0; i <6; i++) {
System.out.print("Tuition fee[" +(i + 1) + "]: ");
tf[i]=in.nextInt();
}
System.out.println("\nTuition fees are: ");
for (int i=0; i<6; i++)
{
System.out.print(tf[i] +", ");

}
}

3.3 Create four arrays and will print the lowest value and specify its index number.

import java.util.Scanner;
public class Perlas_SalvadorFE33 {

static Scanner chris=new Scanner (System.in);

public static void main(String[] args) {

int [] num = {0, 0, 0, 0, 0,};

System.out.println(" ");
for (int i=0; i<num.length; ++i)

11 | P a g e
{
System.out.print("Number [" +i + "]: ");
num[0]= chris.nextInt();
}
int low = num[0];
for (int i=low; i<num.length; i--)
{
if (num[i] < low) low = num[i];
}
System.out.println("The lowest number is: " + low + " at index[" + "]");
}
}

12 | P a g e

Das könnte Ihnen auch gefallen