Quick Links to Application questions (with solutions)!

C++.

Java.

Saturday, April 17, 2010

A program where user can choose... ..to calculate the Sum, Average, Minimum and Maximum of the numbers.


import java.util.Scanner;
import java.util.Random;
class bananas
{
public static void main(String args[])
{
int rnrn,counter=0,one,number[]=new int[10],sum=0;
long time=1;
Scanner eggs = new Scanner(System.in);
System.out.println("If you want to input numbers, hit 1. To generate numbers, hit 2.");
one = eggs.nextInt();
if (one == 1)
{
System.out.println("Enter numbers from 1-50. Enter a zero to end it and continue.");
counter = counter-1;
do
{
counter++;
System.out.println("Enter a number: ");
number[counter] = eggs.nextInt();
}while (number[counter] > 0);
}
else if (one == 2)
{
Random rn = new Random();
rnrn = rn.nextInt(3)+3;
System.out.println("Random numbers are as follow: ");
for(counter=0; counter<=rnrn; counter++)
{
number[counter] = rn.nextInt(49)+1;
System.out.println(number[counter]);
}
}
else
{
System.exit(0);
}

//System.out.println("Number of numbers: " + counter);

for (int counter2=0; counter2 <= counter; counter2++)
{
sum += number[counter2];
}
/*for (int sum2: number)
{
sum+=sum2;
}*/ //Alternative way to write:)
/*
for (int counter2=0; counter2 <= counter; counter2++)
{
sum = sum + number[counter2];
}*/ //Alternative way to write:)

for (int counter2=0; counter2 <= counter-1; counter2++)
{
time = time * number[counter2];
}

System.out.println("Sum of numbers = " + sum);
System.out.println("Multiplication of numbers = " + time);
}
}

//Other Array usages. 1) ++number[]; - Increase by one with every 'hit'.

A program to generate 4 random numbers and calculate the Sum, Average, Minimum and Maximum of the numbers.

import java.util.Random;
class bananas
{
public static void main(String args[])
{
int no1,no2,no3,no4,sum,avg,min,max,a,b,c,d;
Random rn = new Random();
no1 = rn.nextInt(99)+1;
no2 = rn.nextInt(99)+1;
no3 = rn.nextInt(99)+1;
no4 = rn.nextInt(99)+1;
System.out.println("Random numbers are as follow: " +"\n"+ no1 +"\n"+ no2 +"\n"+ no3 +"\n"+ no4);
sum = no1+no2+no3+no4;
avg = (no1+no2+no3+no4)/4;
min = Math.min(a=(Math.min(no1,no2)),b=(Math.min(no3,no4)));
max = Math.max(c=(Math.min(no1,no2)),d=(Math.max(no3,no4)));
System.out.println("Sum:"+sum+"\nAverage:"+avg+"\nSmallerst number:"+min+"\nLargest number:"+max);
}
}

A program to grade a student who has taken a multiple choice test. (a question has 4 options to choose from)

Let's understand the problem first before we start writing.
A student takes a multiple choice test.
A multiple choice question has four options to choose from.
Only one option out of four is the correct answer.
So, the chance of the student getting an incorrect answer in a question is 3 out of 4 or 75%
and the chance of him getting the correct answer is 1 out of 4 or 25%
Therefore,
When we grade the student,
we will use this system.

If the student gets 25% or above,
The student passes the test.
If the student gets 50% or above,
The student gets credit for the course for his commendable achievement.
If the student gets 75% or above,
The student completes the course with distinction.
If the student gets 100%;
The student, of course, has completed the test/exam with full mark.
But if the student unfortunately gets lower than 25%
The student fails the test.

----------


import java.util.Scanner;
class bananas
{
public static void main(String args[])
{
int nq,nca,p;
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of questions in the test.");
nq = input.nextInt();
System.out.println("Enter the number of correct answers in the test.");
nca = input.nextInt();
p = (nca*100)/nq; //it is not p = (nca/nq)*100;
System.out.println("The percentage of correct answers in the question =" + p +"%");
if (p == 100)
System.out.println("The student scores full mark!");
else if (p >= 75)
System.out.println("The student completes the course with distinction.");
else if (p >= 50)
System.out.println("The student passes and gets credit for the course.");
else if (p >= 25)
System.out.println("The student passes the test.");
else
System.out.println("The student fails the test.");
}
}

A program to find volume of a cube.

import java.util.Scanner;
class bananas
{
public static void main(String args[])
{
int x,y,z,area;
Scanner input = new Scanner(System.in);
System.out.println("Enter the length");
x = input.nextInt();
System.out.println("Enter the breadth");
y = input.nextInt();
System.out.println("Enter the height");
z = input.nextInt();
area = x*y*z;
System.out.println("Area = "+ area);
}
}

Application

Questions

  • A program to find volume of a cube.
  • A program to grade a student who has taken a multiple choice test. (a question has 4 options to choose from)
  • A program to generate 4 random numbers and calculate the Sum, Average, Minimum and Maximum of the numbers.
  • A program where user can choose whether to input numbers or let it generate a random number of random numbers to calculate the Sum, Average, Minimum and Maximum of the numbers.

Wednesday, March 17, 2010

Words - Multiple classes + private usage + constructors (with user input) code

import java.util.Scanner;

class bananas
{
public static void main(String[] args)
{
Scanner eggs = new Scanner(System.in);
System.out.println("Enter your first name: ");
String name = eggs.nextLine();
papayas papayasObject = new papayas(name);
papayasObject.one();
System.out.println("Enter your last name: ");
String name2 = eggs.nextLine();
papayas papayasObject2 = new papayas(name2);
papayasObject2.one();
}
}


----------New Class----------


public class papayas
{
private String nameB;
public papayas(String nameA)
{
nameB=nameA;
}
/*Another way to write it:
public String two()
{
return nameB;
}*/ 
public void one()
{
System.out.println("Output: " +nameB+ ".");
/*Another way to write it: 
System.out.printf("Output: %s", two() + "." + "\n");*/
}
}

Words - Multiple classes (with user input) code

import java.util.Scanner;

public class bananas
{
public static void main(String[] args)
{
Scanner eggs = new Scanner(System.in);
papayas papayasObject = new papayas(); //Creating a new class
System.out.println("Enter your name here: ");
String yourname = eggs.nextLine(); //input
papayasObject.one(yourname); //Getting output to output
}
}

----------New Class---------- I named my new class, papayas.

public class papayas
{
public void one(String yourname)
{
System.out.println("Hello, "+ yourname +"!"); //output
}
}

Wednesday, February 17, 2010

Number - Loop statement (with user input) code

import java.util.Scanner;
class bananas
{  
public static void main(String[] args)
{
Scanner egg = new Scanner(System.in);
System.out.println("Enter an integer between 1 and 100: ");
int z = 1, y, x = egg.nextInt();
if ( x>=1 && x<=100 )
{
for(y = 1; y<=100;y++)
{
z = x * y;
System.out.println(z);
}
}
else
{
System.out.println("It is not an integer between 1 and 100.");
}
}
}

Number - While statement (with user input) code - 2nd version

import java.util.Scanner;
class bananas
{
public static void main(String args[])
{
Scanner eggs = new Scanner(System.in);
System.out.println("Enter an interger between 1 and 100.");
int x= eggs.nextInt(), y=1, z=1;
if ( x>=1 && x<=100 )
{
while (y <= 100)
{
z = x * y;
System.out.println(z);
y++;
}
}
else
{
System.out.println("It is not an integer between 1 and 100.");
}
}
}

Number - While statement (with user input) code

import java.util.Scanner;

class bananas
{
public static void main(String args[])
{
Scanner eggs = new Scanner(System.in);
System.out.println("Enter an interger between 1 and 10.");
int x, y, z;
x = eggs.nextInt();
y = 1;
z = 1;
switch (x)
{
case 1:
while (y <= 10)
{
System.out.println(x);
x++;
y++;
}
break;
case 2:
while (y <= 10)
{
z = x * y;
System.out.println(z);
y++;
}
break;
case 3:
while (y <= 10)
{
z = x * y;
System.out.println(z);
y++;
}
break;
case 4:
while (y <= 10)
{
z = x * y;
System.out.println(z);
y++;
}
break;
case 5:
while (y <= 10)
{
z = x * y;
System.out.println(z);
y++;
}
break;
case 6:
while (y <= 10)
{
z = x * y;
System.out.println(z);
y++;
}
break;
case 7:
while (y <= 10)
{
z = x * y;
System.out.println(z);
y++;
}
break;
case 8:
while (y <= 10)
{
z = x * y;
System.out.println(z);
y++;
}
break;
case 9:
while (y <= 10)
{
z = x * y;
System.out.println(z);
y++;
}
break;
case 10:
while (y <= 10)
{
z = x * y;
System.out.println(z);
y++;
}
default:
System.out.println("It is not an integer between 1 and 10.");
}
}
}

Numbers - Switch statement (with user input) code

import java.util.Scanner;

class bananas
{
public static void main(String args[])
{
Scanner eggs = new Scanner(System.in);
System.out.println("Enter an integer between 1 and 10.");
int x;
x = eggs.nextInt();
switch (x)
{
case 1:
System.out.println(x*1);
System.out.println(x*2);
System.out.println(x*3);
System.out.println(x*4);
System.out.println(x*5);
System.out.println(x*6);
System.out.println(x*7);
System.out.println(x*8);
System.out.println(x*9);
System.out.println(x*10);
break;
case 2:
System.out.println(x*1);
System.out.println(x*2);
System.out.println(x*3);
System.out.println(x*4);
System.out.println(x*5);
System.out.println(x*6);
System.out.println(x*7);
System.out.println(x*8);
System.out.println(x*9);
System.out.println(x*10);
break;
case 3:
System.out.println(x*1);
System.out.println(x*2);
System.out.println(x*3);
System.out.println(x*4);
System.out.println(x*5);
System.out.println(x*6);
System.out.println(x*7);
System.out.println(x*8);
System.out.println(x*9);
System.out.println(x*10);
break;
case 4:
System.out.println(x*1);
System.out.println(x*2);
System.out.println(x*3);
System.out.println(x*4);
System.out.println(x*5);
System.out.println(x*6);
System.out.println(x*7);
System.out.println(x*8);
System.out.println(x*9);
System.out.println(x*10);
break;
case 5:
System.out.println(x*1);
System.out.println(x*2);
System.out.println(x*3);
System.out.println(x*4);
System.out.println(x*5);
System.out.println(x*6);
System.out.println(x*7);
System.out.println(x*8);
System.out.println(x*9);
System.out.println(x*10);
break;
case 6:
System.out.println(x*1);
System.out.println(x*2);
System.out.println(x*3);
System.out.println(x*4);
System.out.println(x*5);
System.out.println(x*6);
System.out.println(x*7);
System.out.println(x*8);
System.out.println(x*9);
System.out.println(x*10);
break;
case 7:
System.out.println(x*1);
System.out.println(x*2);
System.out.println(x*3);
System.out.println(x*4);
System.out.println(x*5);
System.out.println(x*6);
System.out.println(x*7);
System.out.println(x*8);
System.out.println(x*9);
System.out.println(x*10);
break;
case 8:
System.out.println(x*1);
System.out.println(x*2);
System.out.println(x*3);
System.out.println(x*4);
System.out.println(x*5);
System.out.println(x*6);
System.out.println(x*7);
System.out.println(x*8);
System.out.println(x*9);
System.out.println(x*10);
break;
case 9:
System.out.println(x*1);
System.out.println(x*2);
System.out.println(x*3);
System.out.println(x*4);
System.out.println(x*5);
System.out.println(x*6);
System.out.println(x*7);
System.out.println(x*8);
System.out.println(x*9);
System.out.println(x*10);
break;
case 10:
System.out.println(x*1);
System.out.println(x*2);
System.out.println(x*3);
System.out.println(x*4);
System.out.println(x*5);
System.out.println(x*6);
System.out.println(x*7);
System.out.println(x*8);
System.out.println(x*9);
System.out.println(x*10);
break;
default:
System.out.println("It is not an integer between 1 and 10.");
}
}
}

Numbers - If statement + Conditional operators (with user input) code

import java.util.Scanner;
class bananas
{
public static void main(String args[])
{
Scanner eggs = new Scanner(System.in);
System.out.println("Enter an integer between 10 and 100: ");
int x;
x = eggs.nextInt();
System.out.println(10 < x && 100 > x ? "It's between 10 and 100." : "It's not between 10 and 100." );
}
}

Numbers - If statement (with user input) code

import java.util.Scanner;
class bananas
{
public static void main(String args[])
{
Scanner eggs = new Scanner(System.in);
System.out.println("Enter an integer between 10 and 100 or -10 and -100: ");
int x;
x = eggs.nextInt();
if (x > 10 && x <100 || x < -10 && x > -100)
if (x > 10 && x <100)
System.out.println("The number you entered is between 10 and 100.");
else
System.out.println("The number you entered is between -10 and -100.");
else if (x < 10 && x > -10)
System.out.println("The number you entered is beteen -10 and 10. It's not between 10 and 100 or -10 and -100.");
else
System.out.println("The number you entered is not between 10 and 100 or -10 and -100.");
}
}

Sunday, January 17, 2010

Words and numbers - Calulation - Adding (with user input) code

import java.util.Scanner;

class bananas
{
public static void main(String args[])
{
Scanner eggs = new Scanner(System.in);
int x, y, ans;
System.out.print("Enter 1st number: ");
x = eggs.nextInt();
System.out.print("Enter 2nd number: ");
y = eggs.nextInt();
System.out.print("Answer: ");
ans = x + y;
System.out.println(ans);
}
}

Words - Sentence (with user input) code

import java.util.Scanner;

class bananas
{
public static void main(String args[])
{
Scanner eggs = new Scanner(System.in); //eggs is just a word I picked.
System.out.println("Type words and system will show you what you typed in another line.");
System.out.println(eggs.nextLine()); //System gets eggs(scanner) and ensures they are words. (1) Eggs gets the input.[in] (2) System prints it out.[out]
System.out.println("Type numbers and system will show you what you typed in another line.");
System.out.println(eggs.nextInt()); //System gets eggs(scanner) and ensures they are numbers. (1) Eggs gets the input.[in] (2) System prints it out.[out]
}
}

Words and numbers - Sentence code

class bananas
{
public static void main(String args[])
{
int bananas; //(int can be substituted with these)
bananas = 3;
System.out.print("I want ");
System.out.print(bananas);
System.out.println(" flowers");
}
}

"Hello, world!" code

class bananas
{
public static void main(String args[])
{
System.out.println("Hello, world!");
}
}

READ ME

Following codes are Java codes.

Thank you for visiting.

-Theo