The Artima Developer Community
Sponsored Link

Java Answers Forum
Java Array Coursework

9 replies on 1 page. Most recent reply: Jan 16, 2003 11:22 PM by Melvin

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 9 replies on 1 page
Tom

Posts: 4
Nickname: grrrd
Registered: Jan, 2003

Java Array Coursework Posted: Jan 10, 2003 7:37 AM
Reply to this message Reply
Advertisement
HI i have had a little trouble with my coursework, and wondering if anyone out there can help. the problem is as follows;
Write a program that carries out a range of mathematical calculations on a variety of integers as input by the program user. The program should:

? Ask the user how many integers they wish to enter i.e. 5
? Asks the user to input the integers i.e. 2, 5, 7, 3, 10 (do not have to be in any order)
? Outputs to screen a menu offering the following calculations based on the integers input:
o The area of a circle with a radius of the given input (area = r2)
o The circumference of a circle with a radius of the given input (circ = 2r)
o The volume of a cube with a width of the given value (v = width3)
o The factorial of the given value
? The use of methods to perform these calculations will gain higher marks
? The user must select which operations they require carried out on the numbers input

When completed the program should ask the user if they wish to run the program again
.

right i can do most of it, but first problem is how do i make the array change its size to how many calculations the user wants to make?

secondly how do i use methods to calculate the sums?

Thanks for even having a look, but response is needed asap, thanks once again


Ugo Posada

Posts: 37
Nickname: binaryx
Registered: Dec, 2002

Re: Java Array Coursework Posted: Jan 10, 2003 10:24 AM
Reply to this message Reply
1. You can't change the size of an array because its static, you could do variuos things:

a. Create an array so large that it can hold all the possible integers the user may want to enter.
b. Use one of the classes of the Collection API to have dynamic data structure that increases its size upon request, i.e: Vector, ArrayList, etc.
c. Create your own dynamic data structure based on arrays, not advisable as you are a beginner.

2. Lets say you have a main method and your array of integers:

public static void main(String[] args) {
        int[] myArray = {1, 2, 3};
        myOwnClass mC = new myOwnClass();
        //This is how you use a method to sum
        mC.sumAllIntegers(myArray);	
    }
 


Now to create the method:

public void sumAllIntegers(int[] myArray) {
    int sum = 0;
    //This loop performs the sum
    for (int i=0; i < myArray.length; i++) {
        sum += myArray[i];
    }
    System.out.println(sum);
}
 

Tom

Posts: 4
Nickname: grrrd
Registered: Jan, 2003

Re: Java Array Coursework Posted: Jan 12, 2003 4:57 AM
Reply to this message Reply
Thanks that does help, one last problem is that i keep on getting compile errors on this bit of code;

int num1;
System.out.print("\n \n How many numbers would you like mathmatical tasks preformed to?");

int arraySize;

arraySize = Easy.getInt();

int[] array = newint[arraySize];

int length = array length();

any ideas??

Ugo Posada

Posts: 37
Nickname: binaryx
Registered: Dec, 2002

Re: Java Array Coursework Posted: Jan 12, 2003 7:21 AM
Reply to this message Reply
> Thanks that does help, one last problem is that i keep on
> getting compile errors on this bit of code;

It is a good idea to copy and paste the compile erros so one can get the idea of whats going on

> int num1;
> System.out.print("\n \n How many numbers would you like
> ke mathmatical tasks preformed to?");
>
> int arraySize;
>
> arraySize = Easy.getInt();
>
> int[] array = newint[arraySize];
>

should be:
 int[] array = new int[arraySize] 


> int length = arraylength();
>

should be:
 int length = array.length(); 


> any ideas??

Tom

Posts: 4
Nickname: grrrd
Registered: Jan, 2003

Re: Java Array Coursework Posted: Jan 12, 2003 2:29 PM
Reply to this message Reply
cannot resolve symbol
symbol : variable Easyln
location: class Coursework
array = Easyln.getInt();

cannot resolve symbol
symbol : variable i
location: class Coursework
array = Easyln.getInt();

cannot resolve symbol
symbol : variable array
location: class Coursework
int length = array.length();

cannot resolve symbol
symbol : variable Easyln
location: class Coursework
arraySize = Easyln.getInt();

whats with all these errors then, this is really starting to annoy me aswell.

people like you make the Internet work, Keep it up and thanks!

Ugo Posada

Posts: 37
Nickname: binaryx
Registered: Dec, 2002

Re: Java Array Coursework Posted: Jan 13, 2003 8:26 AM
Reply to this message Reply
> cannot resolve symbol
> symbol : variable Easyln
> location: class Coursework
> array = Easyln.getInt();
>

This is kind of weird since in a previous code sample you wrote:

 arraySize = Easy.getInt(); 


So, you got the check whether your class instance is called Easy or Easyln or whatever.

> cannot resolve symbol
> symbol : variable i
> location: class Coursework
> array = Easyln.getInt();
>

Where did you declare the i variable, somewhere in your code you must have done something like this:

 int i = 0; 


Or whatever value you want it to have

> cannot resolve symbol
> symbol : variable array
> location: class Coursework
> int length = array.length();
>

array is a variable isn't it? You must notice than an array is not properly a class so you can't apply methods to it. What you have to do is:

 int length = array.length; 


> cannot resolve symbol
> symbol : variable Easyln
> location: class Coursework
> arraySize = Easyln.getInt();
>

Same as before.

> whats with all these errors then, this is really starting
> to annoy me aswell.
>
> people like you make the Internet work, Keep it up and
> thanks!

Thanks to you too.

Regards.

Ugo Posada Zabala
Universidad de los Andes
Bogota - Colombia

Tom

Posts: 4
Nickname: grrrd
Registered: Jan, 2003

Re: Java Array Coursework Posted: Jan 15, 2003 6:37 AM
Reply to this message Reply
class Coursework
{
public static void main (String[ ] args)
{

int arraySize;


System.out.print("Welcome to my program! This progam has been designed as coursework for my first year university course.");


System.out.print("\n\n\n Please enter how many interger numbers you wish mathmatical equations to be preformed on:");



arraySize = Easy.getInt();

int length = arraySize length();

int i=0;

int array=0;

int array = new int[arraySize]

for (int i = 0; i<array length;i++);

{


System.out.print("\n\n\nPlease enter the numbers that you wish to input:");



array = Easy.getInt();
}
char cal;


System.out.print("\n\n\nWhat Calculation would you like to preform?");
System.out.print("\n\nA.The Area of a circle.\nB.The Circumfrence of a circle\nC.The volume");

cal = Easy.getChar();

if cal=a
{
for (int x=0; x<array length;x++)

System.out.print("\n\nThe area of a circle with radius"+array"="+3.141*array^2)

else cal=b;

for (int x=0; x<array length;x++)
System.out.print("\n\nThe circumfrence of a circle with radius" array"="6.282*array)

else cal=c;

for (int x=0; x<array length;x++)

System.out.print("\n\nThe Volume of a cube with sides" array"="array^3);
}
System.out.print("\n\n\n\nEnd of Program"))
}
}




this is my code in its entierity. i still get about 10 errors which i have no idea what so ever to fix.i have racked my brains, books and internet, but still nothing, so if you are able to sort it out i will buy you a E-beer for it.

Chris Skardon

Posts: 2
Nickname: wintermute
Registered: Jan, 2003

Re: Java Array Coursework Posted: Jan 15, 2003 7:31 AM
Reply to this message Reply
for (int i = 0; i<array length;i++);


You should remove the ';' at the end of this for loop, thats one error, what are the other errors?

tata

Ugo Posada

Posts: 37
Nickname: binaryx
Registered: Dec, 2002

Re: Java Array Coursework Posted: Jan 15, 2003 3:52 PM
Reply to this message Reply
> class Coursework
> {
> public static void main (String[ ] args)
> {
>
> int arraySize;
>
>
> System.out.print("Welcome to my program! This progam has
> s been designed as coursework for my first year university
> course.");
>
>
> System.out.print("\n\n\n Please enter how many interger
> r numbers you wish mathmatical equations to be preformed
> on:");
>
>
>
> arraySize = Easy.getInt();
>

Where did you get Easy from? You need to have an instance, unless that getInt() is a static method, however, I think you need to create an instance of the Easy class, you would do this by: Easy a = new Easy, and then call a.getInt().

> int length = arraySize length();
>

Dude, you are mad, why do you want to get the size of an int.


> int i=0;
>
> int array=0;
>
> int array = new int[arraySize]

I guess this is not what you want to do

you are probably trying to:

int[] array = new int[arraySize];
>
> for (int i = 0; i<array length;i++);
>

 for (int i = 0; i<array.length;i++) 

> {
>
>
> System.out.print("\n\n\nPlease enter the numbers that
> t you wish to input:");
>
>
>
> array = Easy.getInt();
> }
> char cal;
>
>
> System.out.print("\n\n\nWhat Calculation would you
> you like to preform?");
> System.out.print("\n\nA.The Area of a circle.\nB.The
> .The Circumfrence of a circle\nC.The volume");
>
> cal = Easy.getChar();
>
> if cal=a

 if (cal == "a") //I dont know very well how to compare chars 


> {
> for (int x=0; x<array length;x++)
>
> System.out.print("\n\nThe area of a circle with
> with radius"+array"="+3.141*array^2)
>
> else cal=b;

 else if (cal=="b") 


>
> for (int x=0; x<array length;x++)
> System.out.print("\n\nThe circumfrence of a circle
> rcle with radius" array"="6.282*array)
>
> else cal=c;

Same as before
>
> for (int x=0; x<array length;x++)
>
> System.out.print("\n\nThe Volume of a cube with
> with sides" array"="array^3);
> }
> System.out.print("\n\n\n\nEnd of Program"))
> }
> }
>
>
>
>
> this is my code in its entierity. i still get about 10
> errors which i have no idea what so ever to fix.i have
> racked my brains, books and internet, but still nothing,
> so if you are able to sort it out i will buy you a E-beer
> for it.

To concatenate strings use the operator +, there are more mistakes in your code but I'm not gonna go trough it again. Just grab a book and start from the beginning dude. Keep the work up!.

Melvin

Posts: 6
Nickname: melvin
Registered: Jan, 2003

Re: Java Array Coursework Posted: Jan 16, 2003 11:22 PM
Reply to this message Reply
Hi,

You may try this.

import java.io.*;

class Work {

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

String line;
int count,userInput;
int array[];
char choice;

System.out.println("Welcome to my program! This progam has been designed as

coursework for my\nfirst year university course.");


System.out.println("\nPlease enter how many interger numbers you wish mathmatical

equations\nto be preformed on:");

BufferedReader br = new BufferedReader (new InputStreamReader (System.in));

line = br.readLine();
count = Integer.valueOf(line).intValue();

array = new int[count];

System.out.println("\n\nPlease enter the numbers that you wish to input:");

for(int i=0; i< count; i++)
{
line = br.readLine();
userInput = Integer.valueOf(line).intValue();
array = userInput;
}



do {


System.out.println("**************************************************" );
System.out.println("\n\nWhat Calculation would you like to preform?");
System.out.println("\n\n1. The Area of a circle.\n2. The Circumfrence of

a circle\n3. The volume\n4. Quit");

choice = (char)System.in.read();


System.out.println("**************************************************");

} while (choice < '1' || choice > '4');

switch(choice) {

case '1' :

for (int x=0;x< count; x++){
System.out.println("\nThe area of a circle with radius

" + array[x]+ " = " + 3.141*array[x]*array[x]);
}
break;

case '2' :

for (int x=0; x<count;x++){
System.out.println("\nThe circumfrence of a circle with

radius " + array[x] + " = " + 6.282*array[x]);
}
break;

case '3' :

for (int x=0; x<count;x++) {
System.out.println("\nThe Volume of a cube with sides "

+ array[x] +" = " + array[x]*array[x]*array[x]);
}
break;

case '4' :
break;

default :
System.out.println("Invalid choice!");
break;

}
System.out.println("\nEnd of Program");
}
}

Flat View: This topic has 9 replies on 1 page
Topic: package   BUG !! ?? Previous Topic   Next Topic Topic: question about java arguments

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use