Please help, its urgent !!

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;
}
}
}
}
 
S

Sabine Dinis Blochberger

TheGodfather said:
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

Yow! On first glance your post looks like spam! Write using proper
capitalization and use punctuation. Then people will have an easier time
to read your plea.

Note that writing in CAPS is considered as SHOUTING in Usenet.

You don't say clearly what you want the program to do, or what the
"algorithm"[sic] is. You also don't specify what goes wrong, what errors
you get etc.

[snipped poorly formatted code]

Also, please format your code to fit in a 80 characters per line limit.
 
R

Roedy Green

class UnArray

The most important comment in a program is the one that goes ahead of
the class. It tells what the program is TRYING to do. Experienced
programers need very few comments about code details. The code itself
explains that. However, without knowing what the program is
attempting, it is very hard to figure out why it is failing.
 
P

Patricia Shanahan

TheGodfather said:
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 ....
//
=============================================================================
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--;
}
}

//
=============================================================================

....

In general, you need to learn how to track problems down in your own
code. See http://home.earthlink.net/~patricia_shanahan/debug/ for an
approach that works for me.

I'm concerned about the lack of method documentation. In particular,
getMax returns the maximum value, but deleteMax uses its max parameter
as the index of the element to delete. Is that intentional?

Patricia
 
L

Lew

Sabine said:
Also, please format your code to fit in a 80 characters per line limit ...

without TAB characters!

And forget about getting urgent replies on Usenet.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top