473,324 Members | 2,257 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,324 software developers and data experts.

Converting an algorythm into Code

65
Hi all,

Im very new to java and getting there slowly.

Im working on basic class functions. Im totally fine with get and set, storing objects into an array and recalling them, but im totally stuck with the following:

declare and set max (using cd[0].getCost())
declare and set maxIndex
for each remaining CD
If CD cost is bigger than max
Set max to bigger value
Set maxIndex
output result

my code so far is (And thanks for any help)
Expand|Select|Wrap|Line Numbers
  1. class CDDriver
  2. {
  3.  
  4.     public static void main(String args[])
  5.     {
  6.  
  7.  
  8.                 CD[] cdList = new CD[10];
  9.  
  10.                 cdList[0] = new CD("Kaiser "," up the khazi ", 9.99);
  11.         cdList[1] = new CD("Oasis "," morning glory ",3.99);
  12.         cdList[2] = new CD("Bob Dylan "," Alreet Sunna ",6.99);
  13.  
  14.  
  15.  
  16.  
  17.         System.out.println("Artist \t\tTitle\t\tCost");
  18.         System.out.println("====================================");
  19.  
  20.                 for (int i = 0; i < cdList.length; i++)
  21.                 System.out.println(cdList[i].getArtist() + " " + cdList[i].getTitle() + " " + cdList[i].getCost());
  22.                // System.out.println(cd1.getArtist() + " " + cd1.getTitle() + " " + cd1.getCost());
  23.                 //System.out.println(cd2.getArtist() + " " + cd2.getTitle() + " " + cd2.getCost());
  24.                // System.out.println(cd3.getArtist() + " " + cd3.getTitle() + " " + cd3.getCost());
  25.  
  26.  
  27.  
  28.     }
  29. }
  30.  
Oct 5 '09 #1
3 1587
r035198x
13,262 8TB
How would you do it "manually"? Imagine looking at a rack of CDs in the shop CDs 'ur us (close to that darn cafe with the fat waiters ...). You want to buy the most expensive CD on that particular rack. How would you do it?

Start from the first CD in the rack, note its price (getCost()) ... <you are supposed to continue the rest of the story here>
Oct 6 '09 #2
whitep8
65
can I just say thank you very much. I was thinking of it lke "show my the highest price", which is the stupid human way to think of it........

This is what i came up with, which works. Can it be improved?

Expand|Select|Wrap|Line Numbers
  1.   // set an integer for the highest price
  2.                       double maxPrice = 0;
  3.  
  4.                         //loop through all records (the cdLists length) and increment by 1
  5.                       for (int i=0; i<cdList.length; i++)
  6.  
  7.                           // if the items price which is being checked is higher than the 
  8.                           //current value of maxPrice, 
  9.                           if (cdList[i].getCost()>maxPrice)
  10.  
  11.                                 //replace its value with that cds price
  12.                           maxPrice = cdList[i].getCost();
  13.  
  14.                       // display max price to the screen
  15.                       System.out.println("The highest price is: " + maxPrice);
  16.  
Oct 6 '09 #3
r035198x
13,262 8TB
Looks OK, assuming all prices are greater than 0 (a reasonable assumption). Without that restriction you would initially assign maxPrice to be the price of the first CD in the list.
Oct 7 '09 #4

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

Similar topics

4
by: Federico Babelis | last post by:
Hi: Any could help my find how to create the code for the following algorythm: In1: 10000000 In2: 00000000000 the algorythm runs and this is the output: 01A6DC6B07262F69
8
by: prabha | last post by:
Hello Everybody, I have to conert the word doc to multiple html files,according to the templates in the word doc. I had converted the word to xml.Also through Exsl ,had finished the multiple...
1
by: Earl Teigrob | last post by:
Does someone have or know of an algorythm (method) that will delete all files under a give directory and its subdirectories based on a wildcard mask? I can use this for one directory for each...
0
by: Andrés Giraldo | last post by:
Hi! I'm trying to use a System.Security.Criptography function, it works on my machine but when I publish the site, it gives me the following error: Can't find a cryptographic service provider...
5
by: Robert | last post by:
I have a series of web applications (configured as separate applications) on a server. There is a main application at the root and then several virtual directories that are independant...
0
by: Bruce H | last post by:
Hello, I want to encrypt files using symmetric cryptography. There are 4 algorythms that the frame supports. Anybody knows which is the fastest algorythm? (big files) TIA
3
by: Mary | last post by:
Hi, Does anyone know of any software out there that would convert an application written in VBScript to either VB.NET or C#/C++ quite quickly for me, or will I have to re-write the application...
7
by: Coleen | last post by:
Does anyone have any good detailed information on the conversion process? We are in the process of converting 2 projects from 2003 to 2005 and have some conversion errors that I can not find...
5
by: plisho | last post by:
Hello, I'm writting a program in C++. One part of my program is to compare two strings and as a result I have to have a number (range:0-1) which represents a similarity between those two strings....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.