473,412 Members | 2,304 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Java help needed.. badly.

6
Hey im stuck on a question that i need to do, i have pretty much the basics of the programme but am not sure how to get it implemented so that it'll work. The question is :

Declare a two dimensional array with 10 rows and 10 columns and initialise:
a) All the elements of its third row to 10
b) All the elements of its 6th column to 20
c) And all the other elements to 5
in that order.

I have the following piece of code that is pretty much the core of the program:

Expand|Select|Wrap|Line Numbers
  1. import uwejava.*;
  2. import glos.*;
  3.  
  4. public class exc4 
  5. {
  6.  
  7.  public int nums;
  8.  
  9.  
  10.  for(int y = 0; y < nums.length; ++y)
  11.   for(int x = 0; x < nums[i].length; ++x)
  12.     if(y == 2)
  13.       nums[y][x] = 10;
  14.     else if(x == 19)
  15.       nums[y][x] = 20;
  16.     else
  17.       nums[y][x] = 5;
  18.   }
  19.  
So if anyone can figure out how to implemet it to work correctly id be delighted. Two solid days of java has pretty much turned my brain to mush :(
Oct 11 '06 #1
3 1411
r035198x
13,262 8TB
Hey im stuck on a question that i need to do, i have pretty much the basics of the programme but am not sure how to get it implemented so that it'll work. The question is :

Declare a two dimensional array with 10 rows and 10 columns and initialise:
a) All the elements of its third row to 10
b) All the elements of its 6th column to 20
c) And all the other elements to 5
in that order.

I have the following piece of code that is pretty much the core of the program:

Expand|Select|Wrap|Line Numbers
  1. import uwejava.*;
  2. import glos.*;
  3.  
  4. public class exc4 
  5. {
  6.  
  7.  public int nums;
  8.  
  9.  
  10.  for(int y = 0; y < nums.length; ++y)
  11.   for(int x = 0; x < nums[i].length; ++x)
  12.     if(y == 2)
  13.       nums[y][x] = 10;
  14.     else if(x == 19)
  15.       nums[y][x] = 20;
  16.     else
  17.       nums[y][x] = 5;
  18.   }
  19.  
So if anyone can figure out how to implemet it to work correctly id be delighted. Two solid days of java has pretty much turned my brain to mush :(
Expand|Select|Wrap|Line Numbers
  1. public class Exc4 {
  2.     public static void main(String[] args) { // every java application has to have this method
  3.         int[][] nums = new int[10][10];//declares a 10 x 10 array of ints
  4.         for(int i = 0; i < nums.length; i++) {
  5.             for(int j = 0; j < nums.length; j++) {
  6.                 if(i == 2) {
  7.                     nums[i][j] = 10;
  8.                 }
  9.                 else if(j == 5) {
  10.                     nums[i][j] = 20;
  11.                 }
  12.                 else {
  13.                     nums[i][j] = 5;
  14.                 }
  15.             }
  16.         }
  17.         for(int i = 0; i < nums.length; i++) {
  18.             for(int j = 0; j < nums.length; j++) {
  19.                 System.out.print(""+nums[i][j]);
  20.             }
  21.             System.out.println();
  22.         }
  23.     }
  24. }
Oct 11 '06 #2
derbys
6
That's brillaint thanks, and opne more question (im a cheeky so and so, i know) I need to

-Create a 50-element integer array
-Fills this array with the random numbers in the range 1 to 10000 and display it.

I *think* i know how i create the integer and random numbers, with the code below - but im not 100% sure on how i should go about displaying it, any help again would be brilliant.

Expand|Select|Wrap|Line Numbers
  1. int[] myArray= new int[50]; // Declaring 50 integers
  2.  
  3. for (int i = 0; i < myArray.length; i++) { //Random number
  4.             Double random = new Double(Math.random() * 10000);
  5.             int randomInt = random.intValue();
  6.             myArray[i] = randomInt;
  7.  
  8.         }
  9.  
I think that's what it is, just need to know what to do to display it

Cheers
Oct 11 '06 #3
r035198x
13,262 8TB
That's brillaint thanks, and opne more question (im a cheeky so and so, i know) I need to

-Create a 50-element integer array
-Fills this array with the random numbers in the range 1 to 10000 and display it.

I *think* i know how i create the integer and random numbers, with the code below - but im not 100% sure on how i should go about displaying it, any help again would be brilliant.

Expand|Select|Wrap|Line Numbers
  1. int[] myArray= new int[50]; // Declaring 50 integers
  2.  
  3. for (int i = 0; i < myArray.length; i++) { //Random number
  4.             Double random = new Double(Math.random() * 10000);
  5.             int randomInt = random.intValue();
  6.             myArray[i] = randomInt;
  7.  
  8.         }
  9.  
I think that's what it is, just need to know what to do to display it

Cheers
Expand|Select|Wrap|Line Numbers
  1. for(int i = 0; i < myArray.length; i++) {
  2.    System.out.print(myArray[i] + " ");// will print all numbers in one line
  3. }
or if you are using jdk 1.5

Expand|Select|Wrap|Line Numbers
  1. for(int i : myArray) {
  2.    System.out.print(i + " ");
  3. }
Oct 12 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

73
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities...
114
by: Maurice LING | last post by:
This may be a dumb thing to ask, but besides the penalty for dynamic typing, is there any other real reasons that Python is slower than Java? maurice
55
by: Elijah | last post by:
I have read many of the topics on learning C++ or Java first. It seems like everyone says something different. I would like to know if I should learn C++ or Java. First a little about myself. I...
5
by: kotkot | last post by:
Hi all - I am looking for a JAVA script that automatically downloads and executes a .exe or .com program upon clicking a link. Let me know (I'd appreciate an answer to my email address as well) ...
19
by: Davey | last post by:
Which is typically faster - a Java server application or a C++ server application?
51
by: erikcw | last post by:
DiveIntoPython.org was the first book I read on python, and I really got a lot out of it. I need to start learning Java (to maintain a project I've inherited), and was wondering if anyone knew of...
0
by: TraceyAnnison | last post by:
I wonder if you can help me - I'm new to this, and working in a project that has already been configured to work with Axis. We have a Java project built with the Spring framework, in order to...
29
by: s0suk3 | last post by:
Hello, I was hoping to get some opinions on a subject. I've been programming Python for almost two years now. Recently I learned Perl, but frankly I'm not very comfortable with it. Now I want to...
49
by: aarklon | last post by:
Hi all, See:- http://www.cs.princeton.edu/introcs/faq/c2java.html for C vs Java in number crunching http://husnusensoy.blogspot.com/2006/06/c-vs-java-in-number-crunching.html
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.