Showing posts with label array. Show all posts
Showing posts with label array. Show all posts

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]); } } }

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? ;)

All in One: SOLVED!

So, now we finished the program we are making - A game. So here's the code, check it out:

import java.io.*;
public class QnA
{
public static void main (String args[]) throws IOException
{
DataInputStream old = new DataInputStream (System.in);

int startexit;

int[][] love = {{3,4,5},{6,7,8,9}};
int[][] heart= {{1,2},{6,7,8,9}};
int[][] ambot = {{1,2},{3,4,5},};

String name;

System.out.println("Enter your name:");
name = old.readLine();


System.out.println("\t \t =============================================== \t \t");
System.out.println("\t \t =============================================== \t \t");
System.out.println("\t \t +++++++++++++ Questions & Answers +++++++++++++ \t \t");
System.out.println("\t \t =============================================== \t \t");
System.out.println("\t \t =============================================== \t \t");

System.out.println("\n\n\n Greetings " + name + "!");

System.out.println ("\n First Round: \n Instruction: In order to proceed to the the second and final round, you have to get 3 correct answers. Each correct answer costs 1 point only. Press only the number of your choice. \n Note: Invalid Choices are considered Wrong.");

System.out.println("\nPress 1 to Start or press any number to exit");
startexit = Integer.parseInt(old.readLine());

if (startexit == 1)
{

System.out.println("\t \t =============================================== \t \t");
System.out.println("\t \t +++++++++++++ Questions & Answers +++++++++++++ \t \t");
System.out.println("\t \t +++++++++++++++++ First Round +++++++++++++++++ \t \t");
System.out.println("\t \t =============================================== \t \t");

int n=0;
int correct=0;

// System.out.println ("\nStage 2");
System.out.println ("\nQuestion # 1");
System.out.println ("\nWhat is the Oldest Street in the Philippines?");
System.out.println ("\n1. Orion St.,C.D.O. City");
System.out.println ("\n2. Sulu St.,Sta Cruz Manila");
System.out.println ("\n3. Colon St.,Cebu");
System.out.println ("\n4. Quezon ave.Manila\n");

n=Integer.parseInt (old.readLine());

switch (n)
{
case 1: System.out.println ("\nWrong");
break;
case 2: System.out.println ("\nWrong");
break;
case 3: System.out.println ("\nCorrect");
correct = correct +1;
break;
case 4: System.out.println ("\nWrong");
break;
default : System.out.println ("\nInvalid Choice");
}


System.out.println ("\nQuestion # 2");
System.out.println ("\nWhat is the most consumed fruit in the world?");
System.out.println ("\n1. Grapes");
System.out.println ("\n2. Bananas");
System.out.println ("\n3. Apple");
System.out.println ("\n4. Mangoes\n");

n=Integer.parseInt (old.readLine());

switch (n)
{
case 1: System.out.println ("\nWrong");
break;
case 2: System.out.println ("\nCorrect");
correct = correct +1;
break;
case 3: System.out.println ("\nWrong");
break;
case 4: System.out.println ("\nWrong");
break;
default : System.out.println ("\nInvalid Choice");
}

System.out.println ("\nQuestion # 3");
System.out.println ("\nWhat is Biggest animal in the world?");
System.out.println ("\n1. blue whales");
System.out.println ("\n2. elephants");
System.out.println ("\n3. Polar Bears");
System.out.println ("\n4. flies and mosquitoes");

n=Integer.parseInt (old.readLine());

switch (n)
{
case 1: System.out.println ("\nCorrect");
correct = correct +1;
break;
case 2: System.out.println ("\nWrong");
break;
case 3: System.out.println ("\nWrong");
break;
case 4: System.out.println ("\nWrong");
break;
default : System.out.println ("\nInvalid Choice");
}

System.out.println ("\nQuestion # 4");
System.out.println ("\nWho is the Greek Goddess of Wisdom?");
System.out.println ("\n1. Apollo");
System.out.println ("\n2. Demeter");
System.out.println ("\n3. Athena");
System.out.println ("\n4. Zeus\n");

n=Integer.parseInt (old.readLine());

switch (n)
{
case 1: System.out.println ("\nWrong");
break;
case 2: System.out.println ("\nWrong");
break;
case 3: System.out.println ("\nCorrect");
correct = correct +1;
break;
case 4: System.out.println ("\nWrong");
break;
default : System.out.println ("\nInvalid Choice");
}

System.out.println ("\nQuestion # 5");
System.out.println ("\nWhat is the largest city in the world?");
System.out.println ("\n1. Tokyo-Yokohama, Japan");
System.out.println ("\n2. Manila, Philippines");
System.out.println ("\n3. New York, United States");
System.out.println ("\n4. Sao Paulo, Brazil\n");

n=Integer.parseInt (old.readLine());

switch (n)
{
case 1: System.out.println ("\nCorrect");
correct = correct +1;
break;
case 2: System.out.println ("\nWrong");
break;
case 3: System.out.println ("\nWrong");
break;
case 4: System.out.println ("\nWrong");
break;
default : System.out.println ("\nInvalid Choice");
}

for (int ctr = 50; ctr>0; ctr--)
{
System.out.println("Loading");
}

System.out.println ("\n\nCorrect answers:" + correct);

if (correct < 3) { System.out.println("Sorry, you failed. You cannot proceed to the final level"); System.out.println("\n\nGoodbye!" + " " + heart[0][0] + love[0][1] + ambot[1][0] + "! :)"); } else { System.out.println("\n\nCongratulations! You passed and you can now proceed to the next level"); int decision; System.out.println("\nDo you wish to continue? Press 1 if yes or press any number if no."); decision = Integer.parseInt(old.readLine()); if (decision == 1) { System.out.println("\t \t =============================================== \t \t"); System.out.println("\t \t +++++++++++++ Questions & Answers +++++++++++++ \t \t"); System.out.println("\t \t +++++++++++++++++ Final Round +++++++++++++++++ \t \t"); System.out.println("\t \t =============================================== \t \t"); System.out.println ("\n Final Round: \n Instruction: In order to win the game, you have to get at least 9 points. Each correct answer costs 2 points only. Type the number of your answer only \n Note: Points in the first round will be added. Invalid Choices are considered Wrong."); int answers; System.out.println("\n\nWhat festival is celebrated in Cebu every third Sunday of January? "); System.out.println("\n1.Sinulog \n2.Durian\n3.Albay\n4.Nile River\n5.San Lorenzo Ruiz"); answers = Integer.parseInt(old.readLine()); switch (answers) { case 1 : System.out.println("Correct!"); correct = correct + 2; break; case 2 : System.out.println("Wrong!"); break; case 3 : System.out.println("Wrong!"); break; case 4 : System.out.println("Wrong!"); break; case 5 : System.out.println("Wrong!"); break; default: System.out.println("Wrong!"); break; } System.out.println("\nWho was the first Filipino Saint? "); System.out.println("\n1.Sinulog \n2.Durian\n3.Albay\n4.Nile River\n5.San Lorenzo Ruiz"); answers = Integer.parseInt(old.readLine()); switch (answers) { case 1 : System.out.println("Wrong!"); break; case 2 : System.out.println("Wrong!"); break; case 3 : System.out.println("Wrong!"); break; case 4 : System.out.println("Wrong!"); break; case 5 : System.out.println("Correct!"); correct = correct + 2; break; default: System.out.println("Wrong!"); break; } System.out.println("\nWhat is the longest river in the world? "); System.out.println("\n1.Sinulog \n2.Durian\n3.Albay\n4.Nile River\n5.San Lorenzo Ruiz"); answers = Integer.parseInt(old.readLine()); switch (answers) { case 1 : System.out.println("Wrong!"); break; case 2 : System.out.println("Wrong!"); break; case 3 : System.out.println("Wrong!"); break; case 4 : System.out.println("Wrong!"); correct = correct + 2; break; case 5 : System.out.println("Wrong!"); break; default: System.out.println("Wrong!"); break; } System.out.println("\nWhere can you find Mayon Volcano?"); System.out.println("\n1.Sinulog \n2.Durian\n3.Albay\n4.Nile River\n5.San Lorenzo Ruiz"); answers = Integer.parseInt(old.readLine()); switch (answers) { case 1 : System.out.println("Wrong!"); break; case 2 : System.out.println("Wrong!"); break; case 3 : System.out.println("Wrong!"); correct = correct + 2; break; case 4 : System.out.println("Wrong!"); break; case 5 : System.out.println("Wrong!"); break; default: System.out.println("Wrong!"); break; } for (int ctr = 50; ctr>0; ctr--)
{
System.out.println("Loading");
}

System.out.println("\n\n\n");

if (correct >=9)
{
System.out.println("Congratulations! You won!");
System.out.println ("\nOverall Score:" + correct);


System.out.println("\t \t ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \t \t");
System.out.println("\t \t ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \t \t");
System.out.println("\t \t ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \t \t");
System.out.println("\t \t ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \t \t");
System.out.println("\t \t ~~~~~~~~~~~~~~~~ Congratulations! ~~~~~~~~~~~~~~ \t \t");
System.out.println("\t \t ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \t \t");
System.out.println("\t \t ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \t \t");
System.out.println("\t \t ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \t \t");
System.out.println("\t \t ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \t \t");
}
else
{
System.out.println("Sorry you failed. Better luck next time");
System.out.println ("\nOverall Score:" + correct);
}
}

else
System.out.println("\n\nGoodbye!" + " " + heart[0][0] + love[0][1] + ambot[1][0] + "! :)");

}


}
else

System.out.println("\n\nGoodbye!" + " " + heart[0][0] + love[0][1] + ambot[1][0] + "! :)");
}
}


Programmers:

All in One

I'm in the middle of creating a program [Java] with the help of two other pals I got in school. We will actually defend that program within this week. We are required to use four out of the five statements, namely:

  • If statement (which includes the Dangling If's and others)
  • Arrays (Whether the normal array or the 2D)
  • Switch (commonly known as Case)
  • Loop (any of the loops)
So array is divided into two which means we have the power to choose which array to use, and that makes it four.

I'll post the code as soon as we are able to merge our codes. Further discussions would be available regarding the program.

Problem 2: Arrays

Based from the previous problem about arrays, here's another thing which is similar to that problem: Create a program that will allow the user to enter ten numbers and sort the numbers in an ascending and descending order. Then, display the highest and the lowest value of the ten numbers given by the user.

import java.io.*;
import java.util.Arrays;

public class Oc13
{
public static void main (String args[]) throws IOException
{

DataInputStream whatever = new DataInputStream (System.in);

int earth[] = new int [10];
int temp;

System.out.println("Enter 10 numbers: ");

for (int i = 0; i< earth.length/2; ++i ) { temp = earth[i]; earth[i] = earth[earth.length - i - 1]; earth[earth.length - i - 1] = temp; } System.out.println("Descending Order: " + Arrays.toString(earth)); System.out.println("Highest is " + earth[0]); // System.out.println("Lowest is " + earth[10]); } }

Problem: Array

Create a program that will allow the user to enter ten numbers and sort the numbers in an ascending and descending order.


import java.io.*;
import java.util.Arrays;

public class Oc13
{
public static void main (String args[]) throws IOException
{

DataInputStream whatever = new DataInputStream (System.in);

int earth[] = new int [10];
int temp;

System.out.println("Enter 10 numbers: ");

for (int i = 0; i< earth.length/2; ++i )

{ temp = earth[i];
earth[i] = earth[earth.length - i - 1];
earth[earth.length - i - 1] = temp; }
System.out.println("Descending Order: " + Arrays.toString(earth));
} }

Arrays


int[] number;
int number

String[] words;
String words;


Try to differentiate the codes above.

The codes on the left side are different from the right. The codes on the left are arrays. What is an array?

An array is similar to any other variables, only that it has its own subarrays and elements. Subarrays are arrays within an array, while elements are found inside the array.


example:

int[] variable = {element, element, element, . . . . .}
or
String[] variable = {{element},{element},{element}}


In declaring an array, the syntax is int[] variable = new Int[size of your array]

Extended Search

Custom Search
Hello World