T
TheGodfather
HI ALL, I M A BEGINNER IN JAVA SO PLEASE IF YOU GET MY PROBLEM PLEASE
HELP OR EVEN FIX IT FOR ME IF IT IS POSSIBLE
MY PROGRAM AND ITS APP WERE WORKING PERFECTLY TILL I ADDED A GETMAX()
AND DELETEMAX() METHODS, THE GET MAX WAS FOR GETTING THE MAXIMUM OF
THE ARRAY THE USER ENTERS AND DELETE MAX WAS FOR DELETING JUST THIS
VALUE,
THE PROGRAM COMPILES PERFECTLY BUT ITS APP DOESNT AND I CANT GET WHERE
THE PROBLEM IS? EVERYTHING ELSE BEFORE ADDING THE BETWEEN(=====) LINES
WAS OK SO I USED (=====) TO SHOW ANYONE WHO WANTS TO HELP ME, WHERE MY
PROB. COMES FROM , AND ALSO PLEASE IF ANY ALGORITHM IS WRONG PLEASE BE
KIND TELLING ME
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class UnArray
{
// Variables
int[] arr; // an array of integers
int nItems; // an integer counting the no. of items in the array
// Constructor
UnArray(int size)
{
arr = new int[size]; // Makes an array with 'size' slots
nItems = 0; // no items yet in the array
}
// Methods
void insert(int item)
{
arr[nItems] = item; // insert the 'item' in the first empty slot
nItems++;
}
int find(int item)
{
int j = 0;
while ( j < nItems )
{
if (arr[j] == item)
{
System.out.println("I found " + item + " in slot " + j);
break;
}
else
{
j++;
if ( j == nItems ) // We have reach the end of the array
{
System.out.println("I did not find " + item
+ " in the array");
}
}
}
return j;
}
void delete(int item)
{
int slot = find(item); // We run the find method, and the
// integer that it returns, we
// store it in variable 'slot'
if ( slot == nItems) // We did not find 'item' in the array
{
System.out.println("DELETION CANCELLED!!");
}
else
{
for (int k = slot+1; k < nItems; k++)
{
arr[k-1] = arr[k]; // Shift upwards the remaining no.s
}
nItems--;
}
}
void display()
{
for (int i = 0; i < nItems; i++)
{
System.out.print(arr + " ");
}
System.out.println();
}
//
=============================================================================
int getMax()
{
int max = arr[0];
for (int i = 0; i < arr.length ; i++)
{
if ( max < arr )
{
max = arr;
}
}
return max;
}
//
=============================================================================
void deleteMax(int max)
{
int slot = max;
{
for (int k = slot+1; k < nItems; k++)
{
arr[k-1] = arr[k];
}
nItems--;
}
}
//
=============================================================================
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
THIS IS THE APPLICATION OF MY PROGRAMM
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import java.io.*;
class UnArrayApp
{
public static void main (String[] args) throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
UnArray the_array = new UnArray(10);
while(true)
{
System.out.println("Press 'i' to insert, 'f' to find, 'd' to
delete, 's' to show,'m' to get max or 'e'to delet max");
String sel = br.readLine();
char choice = sel.charAt(0);
switch(choice) // According to the value of choice, do
// different actions.
{
case 'i':
System.out.println("Enter item to insert:");
sel = br.readLine();
int item = Integer.parseInt(sel);
the_array.insert(item);
the_array.display();
break;
case 'f':
System.out.println("Enter item to find:");
sel = br.readLine();
item = Integer.parseInt(sel);
the_array.find(item);
break;
case 'd':
System.out.println("Enter item to delete:");
sel = br.readLine();
item = Integer.parseInt(sel);
the_array.delete(item);
the_array.display();
break;
case 's':
the_array.display();
break;
//====================================
case 'm':
max.display();
break;
//=========================================
case 'e':
sel = br.readLine();
item = Integer.parseInt(sel);
the_array.deletMax(max);
the_array.display();
break;
//==========================================
default:
System.out.println("Please press 'i', 'f', 'd', or 's'");
break;
}
}
}
}
HELP OR EVEN FIX IT FOR ME IF IT IS POSSIBLE
MY PROGRAM AND ITS APP WERE WORKING PERFECTLY TILL I ADDED A GETMAX()
AND DELETEMAX() METHODS, THE GET MAX WAS FOR GETTING THE MAXIMUM OF
THE ARRAY THE USER ENTERS AND DELETE MAX WAS FOR DELETING JUST THIS
VALUE,
THE PROGRAM COMPILES PERFECTLY BUT ITS APP DOESNT AND I CANT GET WHERE
THE PROBLEM IS? EVERYTHING ELSE BEFORE ADDING THE BETWEEN(=====) LINES
WAS OK SO I USED (=====) TO SHOW ANYONE WHO WANTS TO HELP ME, WHERE MY
PROB. COMES FROM , AND ALSO PLEASE IF ANY ALGORITHM IS WRONG PLEASE BE
KIND TELLING ME
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class UnArray
{
// Variables
int[] arr; // an array of integers
int nItems; // an integer counting the no. of items in the array
// Constructor
UnArray(int size)
{
arr = new int[size]; // Makes an array with 'size' slots
nItems = 0; // no items yet in the array
}
// Methods
void insert(int item)
{
arr[nItems] = item; // insert the 'item' in the first empty slot
nItems++;
}
int find(int item)
{
int j = 0;
while ( j < nItems )
{
if (arr[j] == item)
{
System.out.println("I found " + item + " in slot " + j);
break;
}
else
{
j++;
if ( j == nItems ) // We have reach the end of the array
{
System.out.println("I did not find " + item
+ " in the array");
}
}
}
return j;
}
void delete(int item)
{
int slot = find(item); // We run the find method, and the
// integer that it returns, we
// store it in variable 'slot'
if ( slot == nItems) // We did not find 'item' in the array
{
System.out.println("DELETION CANCELLED!!");
}
else
{
for (int k = slot+1; k < nItems; k++)
{
arr[k-1] = arr[k]; // Shift upwards the remaining no.s
}
nItems--;
}
}
void display()
{
for (int i = 0; i < nItems; i++)
{
System.out.print(arr + " ");
}
System.out.println();
}
//
=============================================================================
int getMax()
{
int max = arr[0];
for (int i = 0; i < arr.length ; i++)
{
if ( max < arr )
{
max = arr;
}
}
return max;
}
//
=============================================================================
void deleteMax(int max)
{
int slot = max;
{
for (int k = slot+1; k < nItems; k++)
{
arr[k-1] = arr[k];
}
nItems--;
}
}
//
=============================================================================
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
THIS IS THE APPLICATION OF MY PROGRAMM
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import java.io.*;
class UnArrayApp
{
public static void main (String[] args) throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
UnArray the_array = new UnArray(10);
while(true)
{
System.out.println("Press 'i' to insert, 'f' to find, 'd' to
delete, 's' to show,'m' to get max or 'e'to delet max");
String sel = br.readLine();
char choice = sel.charAt(0);
switch(choice) // According to the value of choice, do
// different actions.
{
case 'i':
System.out.println("Enter item to insert:");
sel = br.readLine();
int item = Integer.parseInt(sel);
the_array.insert(item);
the_array.display();
break;
case 'f':
System.out.println("Enter item to find:");
sel = br.readLine();
item = Integer.parseInt(sel);
the_array.find(item);
break;
case 'd':
System.out.println("Enter item to delete:");
sel = br.readLine();
item = Integer.parseInt(sel);
the_array.delete(item);
the_array.display();
break;
case 's':
the_array.display();
break;
//====================================
case 'm':
max.display();
break;
//=========================================
case 'e':
sel = br.readLine();
item = Integer.parseInt(sel);
the_array.deletMax(max);
the_array.display();
break;
//==========================================
default:
System.out.println("Please press 'i', 'f', 'd', or 's'");
break;
}
}
}
}