0% found this document useful (0 votes)
11 views

OOP - Class Notes and Programs

The document discusses several Java programs that demonstrate input/output functions and concepts like: 1) Getting user input and displaying output 2) Adding two numbers entered by the user and displaying the total 3) Calculating a student's midterm grade based on class standing and exam scores entered by the user 4) Calculating a student's midterm grade and conditionally displaying a pass or fail message 5) Calculating fare amount for different age groups based on a base fare entered by the user Additional Java concepts like for loops and arrays are also briefly introduced.

Uploaded by

202210215
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

OOP - Class Notes and Programs

The document discusses several Java programs that demonstrate input/output functions and concepts like: 1) Getting user input and displaying output 2) Adding two numbers entered by the user and displaying the total 3) Calculating a student's midterm grade based on class standing and exam scores entered by the user 4) Calculating a student's midterm grade and conditionally displaying a pass or fail message 5) Calculating fare amount for different age groups based on a base fare entered by the user Additional Java concepts like for loops and arrays are also briefly introduced.

Uploaded by

202210215
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Input and Outputs

Tags

Created time @September 16, 2023 1:09 PM

Program 1: Getting Input and giving


output
Main.java

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

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


int num = in.nextInt();

System.out.print("Number is: " + num);


}
}

Phone Pics

Input and Outputs 1


Program 2: Adding numbers

Input and Outputs 2


Prog2.java

import java.util.Scanner;

public class Prog2


{
public static void main (String Args[])
{
Scanner in = new Scanner (System.in);
int num1, num2, tot;

System.out.print("Enter value num 1: ");


num1 = in.nextInt();

System.out.print("Enter value num 2: ");


num2 = in.nextInt();

tot = num1+num2;

System.out.println("The total is: "+tot);


}
}

Output

Phone Pics

Input and Outputs 3


Input and Outputs 4
Input and Outputs 5
Program 3: Midterms Calculator
Grade.java

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

System.out.print("Enter class standing grade: ");


float CS = in.nextFloat();

System.out.print("Enter midterm exam grade: ");


float ME = in.nextInt();

float MG = ((CS*0.60f)+(ME*0.40f)); //use f for float

System.out.println("The total midterm grade is: " + MG);


}
}

Output

Phone Pics

Input and Outputs 6


Input and Outputs 7
Program 4: Midterms Calculator with If-
Else
Main.java

import java.util.Scanner;

public class Main {


public static void main (String Args[])
{
Scanner in = new Scanner (System.in);

System.out.print("Enter class standing grade: ");


float CS = in.nextFloat();

System.out.print("Enter midterm exam grade: ");


float ME = in.nextFloat();

float MG = (float) ((CS*.60)+(ME*.40)); // cascading

if(MG>=75)
{
System.out.println("Your grade is: \t"+MG);
System.out.println("Remarks: You Pass");

}
else
{
System.out.println("Your grade is: \t"+MG);
System.out.println("Remarks: You Failed");
}
}
}

Output

Input and Outputs 8


Phone Pics

Activity 1: Fare Matrix


My Version
Main.java

Input and Outputs 9


// My Version

import java.util.Scanner;

public class Main


{
public static void main (String Args[])
{
Scanner in = new Scanner (System.in);

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


double AGE = in.nextDouble();

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


double Fare = in.nextDouble();

double Fare8 = 0;
double Fare18 = (Fare - (Fare*0.05));
double Fare23 = (Fare - (Fare*0.02));
double Fare59 = Fare;
double Fare60 = (Fare - (Fare*0.10));

if(AGE<=8){
System.out.println("You are free");
System.out.println("Your Fare: "+ Fare8);

}
else if(AGE<=18)
{
System.out.println("You get 5% discount");
System.out.println("Your Fare: "+ Fare18);

}
else if(AGE<=23)
{
System.out.println("You get 2% discount");
System.out.println("Your Fare: "+ Fare23);

}
else if(AGE<=59)
{
System.out.println("You get no discount");
System.out.println("Your Fare: "+ Fare59);

}
else if(AGE>=60)
{
System.out.println("You get 10% discount");
System.out.println("Your Fare: "+ Fare60);

Input and Outputs 10


}
else
{
System.out.println("You have no discount");
System.out.println("Your Fare: "+ Fare);
}
}
}

Output

Kyrie’s Version
Main.java

// Kyrie Adapated version

import java.util.Scanner;

public class Main{


public static void main(String[] args) {
Scanner fare = new Scanner(System.in);

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


int Fare = fare.nextInt();
System.out.print("Enter age: ");
int age = fare.nextInt();

Input and Outputs 11


if (age <= 8) {
System.out.println("Fare is free");

} else if (age <= 18 && age >= 9 ) {


System.out.println("Fare is: " + (Fare - (Fare*0.05)));

} else if (age <= 23 && age >=19) {


System.out.println("Fare is: " + (Fare - (Fare*0.02)));

} else if (age <= 59 && age >=24) {


System.out.println("Regular Fare. " + Fare);

} else if (age >= 60) {


System.out.println("Fare is: " + (Fare - (Fare*0.10)));
}
}
}

Phone Pics

Input and Outputs 12


Additional Discussions

Input and Outputs 13


For Loop

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

System.out.println("Enter Number: \t");


int y = in.nextInt();

for (int x = 1; x<y; x++){


if ((x%2)==0){
System.out.println("Even Number: " + x);
}
}
}
}

Input and Outputs 14


Input and Outputs 15
Array

let x [] = {8,2,3,4}
// first number of array is index 0

ex.
int x [2] = 3
int x [0] = 8

bubble sort

.BubbleSort();
.ASC();
.DSC();

Input and Outputs 16

You might also like