Quick Links to Application questions (with solutions)!

C++.

Java.

Friday, November 11, 2011

Updates!

More games and C++ programs are uploaded on http://parallelgamers.blogspot.com/.
See you there!

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");*/
}
}