You are on page 1of 39

OOPR211 CODECHUM COMPILATION

CODECHUM COMPILATION

BY: CEE JAE DABU OOPR211

JAVA BASICS

JAVA CONDITIONALS

JAVA LOOPING
OOPR211 CODECHUM COMPILATION

JAVA BASICS

1. Leftovers

We Filipinos are quite known for the habit of leaving at least one of
the many pieces of food on the plate as it can't be equally divided
among all at most times. Just like when 3 people decide to eat a
pizza of 10 slices, there will often be at least one slice left on the
box. To accurately show just how many pieces of things are left
when divided by a certain number, we code it using modulo.
Care to do it for me?

Input Output

A line containing two A single line containing an


integers separated by a integer.
space.
OOPR211 CODECHUM COMPILATION

JAVA BASICS

1. Leftovers PROGRAM

import java.util.Scanner;
class Main
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
int num = input.nextInt();
int num2 = input.nextInt();
int num3 = num%num2;
System.out.println(num3);
input.close();
}
}
OOPR211 CODECHUM COMPILATION

JAVA BASICS

2. Three Lines Apart

Do you like reading books? If you do, then you must have
encountered texts that have quite a wide space with dots in
between them, just like pausing or thinking before proceeding the
narration. Let’s try doing that here in C, shall we?

Input Output

Two lines containing a Multiple lines containing a


string on each. string on each.
OOPR211 CODECHUM COMPILATION

JAVA BASICS

2. Three Lines Apart PROGRAM

import java.util.Scanner;

class Main
{

public static void main(String args[])


{

Scanner input = new Scanner(System.in);

String name = input.nextLine();


String name2 = input.nextLine();
System.out.println(name);
System.out.println(".");
System.out.println(".");
System.out.println(".");
System.out.println(name2);
input.close();
}
}
OOPR211 CODECHUM COMPILATION

JAVA BASICS

3. Enumeration

Have you ever answered a test question that tells you to enumerate
a series of things, but the answer must be in a sentence? Quite
tiring to write those things separated by commas and a single
space, isn't it?

Then let's try coding it instead!

Input Output

Five lines containing a A single line containing all


string on each. the strings separated by a
comma and a space.
OOPR211 CODECHUM COMPILATION

JAVA BASICS

3. Enumeration PROGRAM

import java.util.Scanner;

class Main
{

public static void main(String args[])


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

String name = input.nextLine();


String name2 = input.nextLine();
String name3 = input.nextLine();
String name4 = input.nextLine();
String name5 = input.nextLine();
System.out.println(name+", "+name2+", "+name3+", "+name4+",
"+name5);
input.close();
}
}
OOPR211 CODECHUM COMPILATION

JAVA BASICS

4. Body Mass Index

According to the National Heart, Lung, and Blood Institute of the


National Institutes of Health, body mass index (BMI) is a measure of
body fat based on height and weight that applies to adult men and
women. It is used to monitor one's health by determining whether
one is underweight, overweight, has normal weight or is obese. It is
computed based as follows (when using standard weight in pounds
and height in inches):
BMI = 703 x (weight/(height2))

Input Output

Two positive integers The computed BMI of the


representing the weight in appropriate input with 1
pounds and the height in decimal place.
inches.
OOPR211 CODECHUM COMPILATION

JAVA BASICS

4. Body Mass Index PROGRAM

import java.util.Scanner;

class Main
{

public static void main(String args[])


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

float num = input.nextInt();


float num2 = input.nextInt();
float num3 = 703*(num/(num2*num2));
System.out.format("%.1f %n", num3);
input.close();
}
}
OOPR211 CODECHUM COMPILATION

JAVA BASICS

5. Speedy Not Gonzales

The general speed limits in the Philippines in 60 km/h on most


highways and 100 km/h on expressways. Your goal here is to
compute the speed of automobiles based on distance travelled and
time taken to cover this distance.

Input Output

The input is composed of Output should contain 3


four positive integers computed speeds i.e.
representing the distance meters/second,
traveled in meters, and the kilometers/hour, and miles
3 other inputs representing per hour (note that a mile is
the time in hours, minutes, equivalent to 1609 meters)
and seconds it took to cover separated by a space.
the given distance.
OOPR211 CODECHUM COMPILATION

JAVA BASICS

5. Speedy Not Gonzales PROGRAM

import java.util.Scanner;
class Main
{
public static void main(String args[])
{

Scanner input = new Scanner(System.in);

int meter = input.nextInt();


int hr = input.nextInt();
int min = input.nextInt();
int sec = input.nextInt();
double ms, kph, mph;
double tts = (hr*60*60)+(min*60)+sec;
ms = meter / tts;
kph = ms * 3.6;
mph = ms * 2.237;
System.out.format("%.2f "+"%.2f "+"%.2f ",ms,kph,mph );
input.close();
}
}
OOPR211 CODECHUM COMPILATION

JAVA CONDITIONALS

1. Positive or Negative?

When you're feeling good, they call you positive. But when you're
feeling down, they call you negative. Try to determine whether a
number is positive or negative!

Input Output

A line containing an integer. A line containing a string.


OOPR211 CODECHUM COMPILATION

JAVA CONDITIONALS

1. Positive or Negative? PROGRAM

import java.util.Scanner;

class Main {
public static void main(String args[]) {

Scanner input = new Scanner(System.in);

int number = input.nextInt();


if(number > 0)
{
System.out.println("Positive");
}
else if(number < 0)
{
System.out.println("Negative");
}
else
{
System.out.println(number+" is neither positive nor
negative");
}
}
}
OOPR211 CODECHUM COMPILATION

JAVA CONDITIONALS

2. Sum Cubes

The square of an integer refers to the result of multiplying the


integer with itself once. While the cube of an integer refers to the
result of multiplying the integer with itself twice. As long as you
know that, you could easily solve this!

Input Output

A line containing three A line containing a string.


integers separated by a
space.
OOPR211 CODECHUM COMPILATION

JAVA CONDITIONALS

2. Sum Cubes PROGRAM

import java.util.Scanner;

class Main {
public static void main(String args[]) {

Scanner input = new Scanner(System.in);

int number = input.nextInt();


int number2 = input.nextInt();
int number3 = input.nextInt();
int sum = (number*number*number) +
(number2*number2*number2) + (number3*number3*number3);
if(sum > 0)
{
System.out.println("Positive");
}
if(sum < 0)
{
System.out.println("Negative");
}
}
}
OOPR211 CODECHUM COMPILATION

JAVA CONDITIONALS

3. Security Check

Woah there, what are you doing roaming around past the curfew?
I'm going to have to ask for some identification.

Input Output

A line containing an integer. A line containing a string.


OOPR211 CODECHUM COMPILATION

JAVA CONDITIONALS

3. Security Check PROGRAM

import java.util.Scanner;

class Main {
public static void main(String args[]) {

Scanner input = new Scanner(System.in);

int age = input.nextInt();


if(age >= 18)
{
System.out.println("Adult");
}
else if(age < 18)
{
System.out.println("Minor");
}
}
}
OOPR211 CODECHUM COMPILATION

JAVA CONDITIONALS

4. Operation: Operation

You have been cordially invited to partake in Operation: Operation.


Your mission, should you choose to accept it, is to take the two
numbers and the operator given then perform the operation
successfully.

Input Output

A line containing a number, line containing a


operator, and another decimal/float containing
number separated by a two decimal places.
space.
OOPR211 CODECHUM COMPILATION

JAVA CONDITIONALS
4. Operations: Operation PROGRAM

import java.util.Scanner;
class Main {
public static void main(String args[]) {
Scanner input = new Scanner(System.in );
double num1, num2;
char operator;
num1= input.nextDouble();
operator = input.next().charAt(0);
num2= input.nextDouble();
switch(operator)
{
case '+' :
{
double addition = num1 + num2;
System.out.format("%.2f",addition);
break;
}
case '-' :
{
double subtraction = num1 - num2;
System.out.format("%.2f",subtraction);
break;
}
case '*' :
{
double multiplication = num1 * num2;
System.out.format("%.2f",multiplication);
break;
}
case '/' :
{
double division = num1 / num2;
System.out.format("%.2f",division);
break;
}
default :
{
System.out.println("Invalid");
return;
}
}
}
}
OOPR211 CODECHUM COMPILATION

JAVA CONDITIONALS

5. Negative Decimal Tally

We've been giving positive numbers too much attention lately, it's
time to let negative numbers take the spotlight this time!

Input Output

A line containing four A line containing a negative


decimals/floats separated decimal/floats with two
by a space. decimal places.
OOPR211 CODECHUM COMPILATION

JAVA CONDITIONALS
5. NEG DEC TALLY PROGRAM

import java.util.Scanner;
class Main{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
float num = input.nextFloat();
float num2 = input.nextFloat();
float num3 = input.nextFloat();
float num4 = input.nextFloat();

if (num <= 0 && num2 <= 0 && num3 <= 0 && num4 <= 0){
double equal = num + num2 + num3 + num4;
System.out.format("%.2f", equal);
}
else if (num >= 0 && num2 <= 0 && num3 <= 0 && num4 <= 0){
double equal = num2 + num3 + num4;
System.out.format("%.2f", equal);
}
else if (num <= 0 && num2 >= 0 && num3 <= 0 && num4 <= 0){
double equal = num + num3 + num4;
System.out.format("%.2f", equal);
}
else if (num <= 0 && num2 <= 0 && num3 >= 0 && num4 <= 0){
double equal = num + num2 + num4;
System.out.format("%.2f", equal);
}
else if (num <= 0 && num2 <= 0 && num3 <= 0 && num4 >= 0){
double equal = num + num2 + num3;
System.out.format("%.2f", equal);
}
else if (num <= 0 && num2 >= 0 && num3 >= 0 && num4 >= 0){
double equal = num;
System.out.format("%.2f", equal);
}
else if (num >= 0 && num2 <= 0 && num3 >= 0 && num4 >= 0){
double equal = num2;
System.out.format("%.2f", equal);
}
else if (num >= 0 && num2 >= 0 && num3 <= 0 && num4 >= 0){
double equal = num3;
System.out.format("%.2f", equal);
}
OOPR211 CODECHUM COMPILATION

JAVA CONDITIONALS
5. NEG DEC TALLY CONT. PROGRAM

else if (num >= 0 && num2 >= 0 && num3 >= 0 && num4 <= 0){
double equal = num4;
System.out.format("%.2f", equal);
}
else if (num <= 0 && num2 <= 0 && num3 >= 0 && num4 >= 0){
double equal = num + num2;
System.out.format("%.2f", equal);
}
else if (num <= 0 && num2 >= 0 && num3 <= 0 && num4 >= 0){
double equal = num + num3;
System.out.format("%.2f", equal);
}
else if (num <= 0 && num2 >= 0 && num3 >= 0 && num4 <= 0){
double equal = num + num4;
System.out.format("%.2f", equal);
}
else if (num >= 0 && num2 <= 0 && num3 <= 0 && num4 >= 0){
double equal = num2 + num3;
System.out.format("%.2f", equal);
}
else if (num >= 0 && num2 <= 0 && num3 >= 0 && num4 <= 0){
double equal = num2 + num4;
System.out.format("%.2f", equal);
}
else if (num >= 0 && num2 >= 0 && num3 <= 0 && num4 <= 0){
double equal = num3 + num4;
System.out.format("%.2f", equal);
}
else if (num >= 0 && num2 >= 0 && num3 >= 0 && num4 >= 0){
double equal = 00.00;
System.out.format("%.2f", equal);
}
}
}
OOPR211 CODECHUM COMPILATION

JAVA CONDITIONALS

6. Largest Digit

This one is a bit tricky. You're going to have to isolate each digit of
the integer to determine which one is the largest, so good luck!

Input Output

A line containing a three- A line containing a single-


digit integer. digit integer.
OOPR211 CODECHUM COMPILATION

JAVA CONDITIONALS

6. Largest Digit PROGRAM

import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
int num = input.nextInt ();
int rem, Lar= 0;
while (num > 0)
{
rem = num % 10;
if (Lar< rem)
{
Lar= rem;
}
num = num/ 10;
}
System.out.println(Lar);
input.close();
}
}
OOPR211 CODECHUM COMPILATION

JAVA CONDITIONALS

7. Body Mass Index Version 2.0

Recall that according to the National Heart, Lung, and Blood


Institute of the National Institutes of Health, body mass index (BMI)
is a measure of body fat based on height and weight that applies to
adult men and women. It is used to monitor one's health by
determining whether one is underweight, overweight, has normal
weight or is obese. It is computed based as follows (when using
standard weight in pounds and height in inches):
BMI = 703 x (weight/(height2))
Furthermore, people with BMI scores that are less than 18.5 are
said to be underweight. Those with scores between 18.5–24.9
(inclusive) are of normal weight. Those with scores between 25–
29.9 (inclusive) are said to be overweight. And those with scores 30
or higher are obese.

Input Output

A single line containing two


The input is composed of values: the computed BMI
two positive integers with 1 decimal place and
representing the weight in whether the score
pounds and the height in represents underweight,
inches. normal weight, overweight,
or obese.
OOPR211 CODECHUM COMPILATION

JAVA CONDITIONALS

7. Body Mass Index


Version 2.0 PROGRAM

import java.util.Scanner;
class Main {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
double weight, height, BMI;
weight= input.nextDouble();
height = input.nextDouble();
BMI = 703 * ( weight / (height * height));
if(BMI < 18.5){
BMI = 703 * ( weight / (height * height));
System.out.format("%.1f" +" underweight",BMI);
}
else if(BMI <= 24.9){
BMI = 703 * ( weight / (height * height));
System.out.format("%.1f" +" normal weight",BMI);
}
else if(BMI < 29.9){
BMI = 703 * ( weight / (height * height));
System.out.format("%.1f" +" overweight",BMI);
}
else if(BMI > 30){
BMI = 703 * ( weight / (height * height));
System.out.format("%.1f" +" obese",BMI);
}
}
}}
OOPR211 CODECHUM COMPILATION

JAVA CONDITIONALS

8. Speedy, Still Not, Gonzales

Recall that the general speed limits in the Philippines in 60 km/h on


most highways and 100 km/h on expressways. Let's add a minimum
speed when one is in an expressway and that minimum speed limit
is 40 km/h. Your goal here is to compute the speed of automobiles
based on distance travelled and time taken to cover this distance.
And at the same time, whether the driver is over-speeding, within
speed limit, or under-speeding.

Input Output

The input is composed of 3 The output should contain


positive integers the computer speed in km/h
representing the road the (two decimal places) and
driver is on (1 for highway, the status as to whether the
2 for expressway), distance driver is over-speeding,
traveled in meters, and the within-speed-limit, or
number of minutes it took under-speeding.
to cover the given distance.
OOPR211 CODECHUM COMPILATION

JAVA CONDITIONALS

8. Speedy, Still Not


Gonzales PROGRAM

import java.util.Scanner;
class Main {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int road;
double speed, find, fint, dist, time;
road = input.nextInt();
dist = input.nextInt();
time = input.nextInt();
find = dist /1000;
fint = time / 60;
switch(road)
{
case 1 :
{
speed = (find / fint);
if (speed <= 60){
System.out.format("%.2f" + " within-speed-limit",speed);
break;}
}
case 2 :
{
speed = (find / fint);
if (speed <= 60){
System.out.format("%.2f" + " under-speeding",speed);
break;}
}
default :
{
System.out.println("Invalid");
return;
}
}
}
OOPR211 CODECHUM COMPILATION

JAVA CONDITIONALS

9. Visibly Divisible

Do you still know what being divisible by a certain number means?


In case you forgot, being divisible means that the number is capable
of being divided by another number without a remainder.

Input Output

A line containing an integer. A line containing a string.


OOPR211 CODECHUM COMPILATION

JAVA CONDITIONALS

9. Visibly Divisible PROGRAM

import java.util.Scanner;
class Main{
public static void main(String args[]){
Scanner input = new Scanner(System.in);

int num = input.nextInt();

if (num % 3 == 0 && num % 4 == 0){


System.out.println("Divisible by 3 and 4");
}
else if (num % 4 == 0){
System.out.println("Divisible by 4");
}
else if (num % 3 == 0){
System.out.println("Divisible by 3");
}
else{
System.out.println("No Divisibility");
}
}
}
OOPR211 CODECHUM COMPILATION

JAVA CONDITIONALS

10.Sort It Yourself

A descending order means values arranged from largest to smallest.


But in your case, you're going to have to sort these integers in
ascending order, which means from smallest to largest.

Input Output

A line containing three A line containing three


integers separated by a integers separated by a
space. space.
OOPR211 CODECHUM COMPILATION

JAVA CONDITIONALS

10. Sort It Yourself PROGRAM

import java.io.*;
import java.util.Scanner;
import java.util.Arrays;
class Main {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int[] a = new int[3];
int temp=0;

for(int l=0;l<3;l++)
{
a[l]=input.nextInt();
}

for(int index=0;index<a.length;index++)
{
for(int z=index+1;z<a.length;z++)
{
if(a[index] > a[z] )
{
temp = a[index];
a[index] = a[z];
a[z] = temp;
}
}
}

for(int v=0;v<a.length;v++)
{
System.out.print(a[v]+" ");
}
}
}
OOPR211 CODECHUM COMPILATION

JAVA CONDITIONALS

11.The FizzBuzz Game

Let's play a game of FizzBuzz! It works just like the popular


childhood game "PopCorn", but with rules of math applied since
math is fun, right? Just print out "Fizz" when the given number is
divisible by 3, "Buzz" when it's divisible by 5, and "FizzBuzz" when
it's divisible by both 3 and 5!

Let the game begin!

Input Output

A line containing an integer. A line containing an string.


OOPR211 CODECHUM COMPILATION

JAVA CONDITIONALS

11. The FizzBuzz Game PROGRAM

import java.util.Scanner;
class Main{
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int a = input.nextInt();
if(a % 3 == 0 && a % 5 == 0) {
System.out.println("FizzBuzz");
} else if (a % 5 == 0) {
System.out.println("Buzz");
} else if (a % 3 == 0) {
System.out.println("Fizz");
}
}
}
OOPR211 CODECHUM COMPILATION

JAVA CONDITIONALS

12.Is it you, Cody?

Can you identify if Cody's name is spelled right? If so, then make a
program that accepts four one-letter strings separated by space
and print "Correct" if the inputted strings, combined in order, spell
the name Cody correctly. If not, print "Wrong".

It doesn't matter if it's in uppercase or lowercase, as long as it's


spelled correctly, it's considered correct.

Now, will you take on this task?

Input Output

A line containing four one- A line containing an string.


letter strings separated by a
space.
OOPR211 CODECHUM COMPILATION

JAVA CONDITIONALS
12. Is it you, Cody? PROGRAM

import java.util.Scanner;
class Main{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
String a;
String b;
String c;
String d;
a = input.next();
b = input.next();
c = input.next();
d = input.next();
if(a.equals("c") || a.equals("C"))
{
if(b.equals("o") || b.equals("O"))
{
if(c.equals("d") || c.equals("D"))
{
if(d.equals("y") || d.equals("Y"))
{
System.out.println("Correct");
}
else
{
System.out.println("Wrong");
}
}
else
{
System.out.println("Wrong");
}
}
else
{
System.out.println("Wrong");
}
}
else
OOPR211 CODECHUM COMPILATION

JAVA LOOPINGS

1. Broken Record

Looping strings works like playing a broken record – it repeats


something over and over again. But what's great about loops is that
we can define as to how many times it will repeat something with
proper code, and that's what we're about to do now using while()
loops.

Input Output

A line containing an integer. A line containing an string.


OOPR211 CODECHUM COMPILATION

JAVA LOOPING
1. Broken Record PROGRAM

import java.util.Scanner;

class Main {
public static void main(String args[]) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();
int i = 1;
while(i <=n) {
System.out.print("CodeChum is awesome
\n");
i++;
}
}
}
OOPR211 CODECHUM COMPILATION

HAHAHA STARTED LATE


AND WALA NA YUNG
NET DITO PASENSYA NA
PO MAAM

You might also like