The Big Programming Thread - Page 176
Forum Index > General Forum |
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. | ||
lolmlg
619 Posts
| ||
![]()
white_horse
1019 Posts
On October 15 2012 11:08 lolmlg wrote: Did you mean to test i % j, white_horse? Yeah. The first code I put up was really bad. It was my first try. I worked on it after I put a post, and I have something like this now:
But its still weird. Can somebody help me -_______________- thank you very much | ||
lolmlg
619 Posts
Second, you need to think a little bit more about the test you want to use to check for primality. When j is equal to 2, 15 % 2 != 0 and 15 % 2 != 0. Is 15 prime? | ||
mmp
United States2130 Posts
The use of a square root function saves you time, because consider any factor of a number N, call it a. Its cofactor, b, (a * b == N), is such that a <= sqrt(N) or b <= sqrt(N), but not both. Does that make sense? So when you're looking for all of the factors of a number N, you can iterate up to sqrt(N), checking a < sqrt(N), and its cofactor b = N/a. This is faster than iterating all the way to N. | ||
mmp
United States2130 Posts
| ||
Cambium
United States16368 Posts
On October 15 2012 10:56 white_horse wrote: Ok guys I have this project where if I input two numbers anywhere between 4 and 1 million, the program outputs all the prime numbers between the two. Well I got close to it but the program outputs but its really weird still....can you guys help me...... The professor talked about using square root function but I have no idea how. Here is the computational part:
But its still weird. Can somebody help me -_______________- thank you very much Have you heard of this? http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes This is basically a textbook problem asking to be solved with Sieve of Eratosthenes. You just use the bigger number, then loop through the values between the two and output the ones that are true. | ||
Cambium
United States16368 Posts
You then call it with the range of the two numbers you are given, which makes this horribly inefficient. | ||
mmp
United States2130 Posts
In practice, big prime factorization (cryptographic standard) is done using sieve techniques. | ||
mmp
United States2130 Posts
| ||
DeltaX
United States287 Posts
On October 15 2012 10:54 NeMeSiS3 wrote: gah help!!!! I have my midterm tmr and I've been stressing all night going over previous notes but I'm stuck, this is a previous midterm. (the other questions I got relatively easy) + Show Spoiler + A company sells auto insurance has hired you to write a Java program to help with this. You decide to begin by writing two java classes. First, you must write a class that can be used to represent the diriver of an automobile; you decide to call that class "Driver" (not to be confused with a "test" driver). For each Driver, you need to know their name and their age. When a Driver object is first created you must always record their name and their current age. Provide accessor methods for both Driver attributes. Also provide a mutator method to record the fact that the driver has just had a birthday ( i.e. they are now one year older). You must also create and automobile class. This class will be used to represent and automobile that is insured by your client's company. For each automobile we need to record the model year (e.g. 2005), and name of the manufacturer(e.g. General Motors). We also need to know who will be the primary driver for the automobile. Include three (and only three) instance variables in the automobile class. A constructor method should be provided for the automobile class; the constructor willl accept three parameters and use them to inialize the instance variables. You do NOT need to write the accessor and mutator methods for each instance variable in the automobile class, and you do NOT need to include a toString() method. However, you do need to provide one accessor method that will calculate and retrieve the insurance amount for the automobile. The base amount that your client's company charges for auto insurance is $900. However, for older automobiles the cost is higher; specifically, for an automobile with a model year prior to 2002 they charge an extra $50. Driver age is also a factor, if the primary driver is under 25 years of age, the company charges an additional $200. You DO NOT need a test driver program. I have no idea how to approach the automobile class. Here's what I got so far. DRIVER + Show Spoiler + public class driver { private String name; private int age; public driver(String nameIn, int ageIn) { name = nameIn; age = ageIn; } public String getName() { return name; } public int getAge() { return age; } public void setAGE(int age) { age = age++; } } AUTOMOBILE + Show Spoiler + public class Automobile { private int model; private String manufacturer; public Automobile(int modelIn, String manufacturerIn) { model = modelIn; manufacturer = manufacturerIn; } } First off I would say your driver class is pretty close, but age = age++;should prolly be age = age + 1;or just age++;(I was actually not sure if what you had would work or not, so I tried it and it didn't) For your automobile class, it needs 3 (and only 3) instance variables, but you only have 2. You need a 3rd that will represent a driver of said car. (where could we get one of these?) Once you get this the rest of the problem should be straightforward. | ||
mmp
United States2130 Posts
On October 15 2012 10:54 NeMeSiS3 wrote: + Show Spoiler + public class Automobile { private int model; private String manufacturer; public Automobile(int modelIn, String manufacturerIn) { model = modelIn; manufacturer = manufacturerIn; } } What you're doing with modelln to avoid name collision suggests you don't understand how scope works.
Your instructor shouldn't take off points for that, but it will raise an eyebrow. | ||
n.DieJokes
United States3443 Posts
| ||
Amnesty
United States2054 Posts
Anyway, i just made this last night for someone else so it seemed fitting to post it here since the disscussion about primes. Finds primes from 2-4 million well under a second. You will need Visual Studio 2012 If you are a student, and i imagine you are since you said it was a project you can download VS2012 for free at https://www.dreamspark.com/ after you register with your school email address.
| ||
Blisse
Canada3710 Posts
A company sells auto insurance has hired you to write a Java program to help with this. You decide to begin by writing two java classes. First, you must write a class that can be used to represent the diriver of an automobile; you decide to call that class "Driver" (not to be confused with a "test" driver). For each Driver, you need to know their name and their age. When a Driver object is first created you must always record their name and their current age. Provide accessor methods for both Driver attributes. Also provide a mutator method to record the fact that the driver has just had a birthday ( i.e. they are now one year older). You must also create and automobile class. This class will be used to represent and automobile that is insured by your client's company. For each automobile we need to record the model year (e.g. 2005), and name of the manufacturer(e.g. General Motors). We also need to know who will be the primary driver for the automobile. Include three (and only three) instance variables in the automobile class. A constructor method should be provided for the automobile class; the constructor willl accept three parameters and use them to inialize the instance variables. You do NOT need to write the accessor and mutator methods for each instance variable in the automobile class, and you do NOT need to include a toString() method. However, you do need to provide one accessor method that will calculate and retrieve the insurance amount for the automobile. The base amount that your client's company charges for auto insurance is $900. However, for older automobiles the cost is higher; specifically, for an automobile with a model year prior to 2002 they charge an extra $50. Driver age is also a factor, if the primary driver is under 25 years of age, the company charges an additional $200. You DO NOT need a test driver program.
This really shouldn't have given you any trouble. This is the reason I don't like Java as a first language. You're throwing people head first into Classes without teaching them how to problem solve in the first place. Teach them to solve problems first, then add Classes and OOP as different methods of solving problems, instead of confusing students with both. | ||
Amnesty
United States2054 Posts
| ||
mmp
United States2130 Posts
| ||
Blisse
Canada3710 Posts
On October 15 2012 14:35 Amnesty wrote: Derp posted too soon.. Anyway, i just made this last night for someone else so it seemed fitting to post it here since the disscussion about primes. Finds primes from 2-4 million well under a second. + Show Spoiler +
If this actually runs in less than a second, whoa, nice! I will save this somewhere XD On October 15 2012 14:49 mmp wrote: Try not to post full solutions to problems that are for classes. He said it was for midterm studying so I thought it was okay. | ||
WerderBremen
Germany1070 Posts
On October 15 2012 14:37 Blisse wrote: + Show Spoiler + A company sells auto insurance has hired you to write a Java program to help with this. You decide to begin by writing two java classes. First, you must write a class that can be used to represent the diriver of an automobile; you decide to call that class "Driver" (not to be confused with a "test" driver). For each Driver, you need to know their name and their age. When a Driver object is first created you must always record their name and their current age. Provide accessor methods for both Driver attributes. Also provide a mutator method to record the fact that the driver has just had a birthday ( i.e. they are now one year older). You must also create and automobile class. This class will be used to represent and automobile that is insured by your client's company. For each automobile we need to record the model year (e.g. 2005), and name of the manufacturer(e.g. General Motors). We also need to know who will be the primary driver for the automobile. Include three (and only three) instance variables in the automobile class. A constructor method should be provided for the automobile class; the constructor willl accept three parameters and use them to inialize the instance variables. You do NOT need to write the accessor and mutator methods for each instance variable in the automobile class, and you do NOT need to include a toString() method. However, you do need to provide one accessor method that will calculate and retrieve the insurance amount for the automobile. The base amount that your client's company charges for auto insurance is $900. However, for older automobiles the cost is higher; specifically, for an automobile with a model year prior to 2002 they charge an extra $50. Driver age is also a factor, if the primary driver is under 25 years of age, the company charges an additional $200. You DO NOT need a test driver program.
This really shouldn't have given you any trouble. This is the reason I don't like Java as a first language. You're throwing people head first into Classes without teaching them how to problem solve in the first place. Teach them to solve problems first, then add Classes and OOP as different methods of solving problems, instead of confusing students with both. I think you get a good point, I would highly recommend everybody to start even with the basics of C (simple programs, loops, functions etc) and then switch to C++ (classes, methods, heredity, working with files, space management) and realize the diffenrences. Then you've got a good understanding what you are actually doing. Thats at least my point of view, but I'm electrical engineer and not a pure programmer though. | ||
![]()
white_horse
1019 Posts
I got this far now:
but its still not working. could you guys let me know what I'm doing wrong? | ||
mmp
United States2130 Posts
On October 16 2012 09:31 white_horse wrote:
its still not working What happens when i = 5?
Do you see something wrong here? o_O + Show Spoiler + + Show Spoiler + Why do you print out (i + 1) is prime instead of i itself? | ||
| ||