Bubble Sort Practice

The Catch:
Create a program that will sort ten numbers in a descending order.

Solution:
import java.io.*;
class bubblepractice
{
public static void main (String args[]) throws IOException
{
BufferedReader buff = new BufferedReader (new InputStreamReader (System.in));

System.out.println("Enter 10 numbers: ");
int[] numbers = new int [10]; // This signals that ten numbers will be entered by the user

for (int index=0; index<10; index ++) { numbers[index] = Integer.parseInt(buff.readLine()); // Every number entered by the user will be stored in the index of the array numbers } for (int total = 0; total < 10; total++) { for (int two = total + 1; two < 10; two++) // Variable two is the second number next to variable total { if (numbers[two] > numbers[total])
{
int container = numbers[two]; // Initialize the value of two in container if two is bigger than total
numbers[two] = numbers[total]; // store the value of total in two
numbers[total] = container; // store the value of total in the container
}
}
}

System.out.println("To put them in order...");

for (int display = 0; display<10; display++) { System.out.println(numbers[display]); } } }

Bubble Sort

Bubble Sort is the simplest way in sorting algorithms. It maybe not be that efficient but as a beginner, it is a must to study it since it steps through a list, arranging it until it appears in the right order.

For example, arrange the numbers 5,3,1,4, and 2.

Let's do this step by step.

  1. Get the first two numbers (5 and 3). Is 5 greater than 3? Yes. Swap the numbers and it becomes 3,5,1,4,2
  2. 3 remains in the first spot after swapped by 5. Compare 5 and 1. Is five greater than 1? Yes. Swap both numbers and it becomes 3,1,5,4,2.
  3. Proceed to the next step by comparing 5 and 4. Is five greater than 4? Yes, then swap the two numbers and it becomes 3,1,4,5,2.
  4. Is five greater than 2? Yes, then swap. It becomes 3,1,4,2,5
As you've noticed, it is not in the right order yet. We need bubble sort to work on that.

The idea of bubble sort is: by the use of three variables and nested loops, you can work on this comparisons to store each value on each variable until the correct order displays.

Exercise: Using TWO classes

Create a program that will allow the user to input his desired number of records to store with the use of arrays.

example:

Enter Number of records: 1

Name Course Year
Ethel Grace Bobis                                   B.S Computer Science1


Let's start with our the first class. Let's name it Sample2.


class Sample2
{
String Arr1[];
String Arr2[];
String Arr3[];
int nItems;

Sample2(int size)
{
Arr1 = new String[size];
Arr2 = new String[size];
Arr3 = new String[size];
nItems=0;
}

void insert (String val)
{
Arr1[nItems] = val;
Arr2[nItems] = val;
Arr3[nItems] = val;
nItems++;
}

void display ()
{
System.out.println("\t **** \t");
System.out.println("The values are: ");
System.out.println("Name" + "\t\t" + "Course" + "\t\t" + "Year");

for (int x = 0; x <= nItems;x++) { System.out.println(Arr1[x] + "\t\t" + Arr2[x] + "\t\t" + Arr3[x]); } } }



And with the second class, let's name it MClass2


import java.io.*;
public class MClass2
{
public static void main (String args[]) throws IOException
{
BufferedReader m = new BufferedReader( new InputStreamReader(System.in));

System.out.println("Enter number of Records: ");
int rec = Integer.parseInt(m.readLine());

Sample2 y = new Sample2 (rec);

for (int x=1; x<= rec; x++) { { System.out.println("Enter Name"); String name = m.readLine(); y.insert(name); } { System.out.println("Enter Course: "); String course = m.readLine(); y.insert(course); } { System.out.println("Enter Year: "); String year = m.readLine(); y.insert(year); } } y.display(); } }


What's the output? The same above isn't it? ;)

Using two or more classes

Try to open your JCreator, or whatever third party java application you have. You can also use plain notepad if you want and play it through the use of your CMD.

Let's use Sample as the class name for this one...


class Sample
{
int sum;

Sample()
{
sum = 0;
}

void getSum(int num1, int num2)
{
sum = num1+num2;
}

void display()
{
System.out.println("The sum is:" + sum);
}
}


Try to create a new class with the filename of MClass (without deleting your first class which is the sample), and paste the code below:


import java.io.*;
public class MClass
{
public static void main (String args[]) throws IOException
{
BufferedReader m = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter First Number: ");
int a = Integer.parseInt(m.readLine());
System.out.println("Enter Second Number: ");
int b = Integer.parseInt(m.readLine());

Sample x = new Sample();
x.getSum(a,b);
x.display();
}
}


What's your output? Interesting, isn't it? :D

Object Oriented Programming

From the word object,it is understood that you can touch and feel something from it. But unlike in the real world, object is different in programming.

A couple of things to remember in Object Oriented Programming are the following:

  • State 
    • also known as attributes
    • similar to variables (part of your object)
  • Behaviour
    • What your Object can do
    • Methods (part of your object)
      • Non Returning
      • Returning
  • Class
    • the blueprint of your program
  • Constructor Method
    • should be the same of the class and file name
    • they are the ones who makes up the object
    • is used to initialize the behaviour
  • Instantation
    • used in creating a new object
  • Parameters
    • used to pass or receive values
    • enclosed with an open and close parenthesis ()
Try to familiarize these words... for a better future in programming ;)

Extended Search

Custom Search
Hello World