This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Joe Ess

How to i go about .....  RSS feed

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all~
It may be long but I am totally new to this java programming...
i will be thankful if any kind soul can help me out on this :
a)I wish to draw 7 rectangles in a window to show integer array. The height of the rectangle will represent the value of an element of an array. The first element of the array will be represented by the left-most rectangle. This could represent an array whose smallest element is the second to last and whose largest element is the fourth.
I would like to come out with a procedure that will take integer array as input and draw the 7 rectangles in a row. Assume that the all are of the same length (50 elements),and the values of the integers on the array will vary between 1 and 50. But can be improved further to make it work with different sizes of arrays and vary the width of rectangles with the number of elements in the array,and vary the computation of the heights depending on the range of values.
b)How do i implement new algorithms in java, using MergeSort and SelectSort as examples?
C)And a procedures that takes an int input,n, and generates an int array of n random ints with values between 1 and n.It will be better if you have the procedure ensure that no value appears more than once.
d)My last question is that are there ways to draw rectangles using different types of sorting proceduces? Adapt the algorithm so that it repaints the screen senibly : this will depend on the algorithm. My intention is to use the algorithm to sort out the arrays. Any ideas?
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like you have a bunch of problems to solve. Try one at a time!
I'd probably start by learning how to draw since I've never done Java drawing. Guessing that maybe I should draw four lines, maybe I'd make a method like this:

And I'd make a test program that can set up a drawing area and draw a rectangle:

Then I might deal with the array of heights ...

Then maybe scale the height so the tallest one always touches the top

Finally the sorting issue. If it's part of your assignment to write a sort, google for java sort algorithms. Bubble sort is far and away the easiest to understand and works fine for a small number of values. Quicksort is serious computer science stuff. You might copy the code from somewhere, but I wouldn't bother understanding it. You don't write fast sorts, you BUY them. And if you don't have to write your own, Java has some Collection types that sort, tho you'll have to use Integer objects instead of int natives.
I'm betting I'm wrong about how to draw rectangles and you'll find something in Java2D docs that already draws them nicely. But see if the progression of problem solving helps at all. Check in again when you have some code going - or not going.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!