473,785 Members | 2,737 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting an algorythm into Code

65 New Member
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 1607
r035198x
13,262 MVP
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 New Member
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 MVP
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
1776
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
5745
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 output html files. The problem is while reading through the worddoc paragraph,the special characters are not identified. So in the xml file,it's just storing that as "?".So I couldn't able to retrive the characters in my ouput html files.
1
383
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 file in Directory.GetFiles("pic1.*") file.Delete() but will need to use a recursive algorythm to get all its subdirectories
0
1675
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 (CSP) in this algorythm... Any Idea of what's wrong??? Thanks
5
2527
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 applications. I am testing an upgrade of all of the sites and have converted the main root site...although not necessarily fixed any issues. I move on instead and converted one of the virtual roots that is a seperate
0
987
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
2428
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 myself. Any help much appreciated.
7
2501
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 answers to. First, we have a lot of UserControls. I am getting the error "Type uc_mc_btn_footer is not defined." where uc_mc_btn_footer is the name of the UserControl. Also, on almost all of our UserControls the conversion has remmed out all my code...
5
2413
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. I don't need a solution in C++, I just need a hint which algorythm to use to implement this concept. I searched Web, but there is a million of algorythms and I don't know which to use. Thanx for any help!!!
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10090
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9949
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8971
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.