Whats up guys. Got a programming exam coming up on Tuesday and I find it pretty hard so I was wondering if I posted the odd question here would you guys help me. I need to pass the exam so I don't have to do a repeat exam in Augest.
All code is done in Java.
The first problem I have is this:
[code]Use loops appropriately to print the following rectangle of '+' symbols.
++++++
+ +
+ +
+ +
+ +
++++++
[/code]
Now I know this will be laughably simple to alot of you guys but I am only starting out the course really and I am getting a problem with it.
Here is my code [quote]
public class plus
{
public static void main(String [] args)
{
int size = 6;
for(int i = 0; i < size; i++)
System.out.print ("+");
for(int i = 0; i < size - 1; i++)
System.out.println ("+ +");
for(int i = 0; i < size; i++)
System.out.print ("+");
}
}
[quote]
When I run this I get a box like this:
[code]
+++++++ +
+ +
+ +
+ +
+ +
++++++
[/code]
I don't understand why it is doing this, any help would be appreciated. Sorry for the newbie sorta stuff.
*FOR SOME REASON THE BOXES ARE MEANT TO JUST BE SQUARE, THERE IS NOT MEANT TO BE A SECOND LINE OF + SIGNS AFTER THE 1ST, FACEPUNCH WONT LET ME CHANGE IT*
Print a "\n" before printing the "+ +"s
What does that do? I don't think we have ever covered that before?
Inserts a newline character
[editline]13th January 2011[/editline]
An alternative would be storing the first (and last) row to a string and then printing it with a println all at once after it's done.
Also your teacher is an idiot if he hasn't told you what \n does.
[QUOTE=TerabyteS;27390674]Also your teacher is an idiot if he hasn't told you what \n does.[/QUOTE]Afaik teachers usually cover special characters during file I/O lessons. I think he is supposed to output an empty string using println.
Yeah I don't think we can do that really. And in terms of him being an idiot hes actually very smart.
In terms of a new line, I think we use System.out.println(); to create a new line.
You could do something like this:
[code]printf("++++++\n");
for(int i = 0; i < size; i++) printf("++\n");
printf("++++++\n");[/code]
[QUOTE=Over-Run;27393117]Yeah I don't think we can do that really. And in terms of him being an idiot hes actually very smart.
In terms of a new line, I think we use System.out.println(); to create a new line.[/QUOTE]
Might as well do it, get extra points for outside study.
It didn't work when I just tried it there :S
[editline]13th January 2011[/editline]
Also guys I was wondering if you can tell me how this code is working [quote]
int [] a = {12, 4, 11, 15, 0, 19, 6, 7, 8, -10};
int i = 0;
while(i < a.length)
}
a[i] = a[i] + 1;
if(a[i] > 10)
}
i++;
{
[/quote]
I know the answer for it as I have the solutions but I am not too sure on why it is happening.
The array will change to
{13, 11, 12, 16, 11, 20, 11, 11, 11, 11}
I thought what it was doing was if the number in the array is greater than ten it will add 1 onto it. But it doesn't seem to be that!
[QUOTE=Over-Run;27393801]It didn't work when I just tried it there :S
[editline]13th January 2011[/editline]
Also guys I was wondering if you can tell me how this code is working
I know the answer for it as I have the solutions but I am not too sure on why it is happening.
The array will change to
{13, 11, 12, 16, 11, 20, 11, 11, 11, 11}
I thought what it was doing was if the number in the array is greater than ten it will add 1 onto it. But it doesn't seem to be that![/QUOTE]
that code is completely messed up
if looks like this:
if(1 == 1)
{
}
not if(1==1)
}
{
I never knew you could make an array like this:
int [] a = {12, 4, 11, 15, 0, 19, 6, 7, 8, -10};
Might become useful.
[QUOTE=Over-Run;27393801]
I thought what it was doing was if the number in the array is greater than ten it will add 1 onto it. But it doesn't seem to be that![/QUOTE]
This code does that:
[cpp]
int[] a = {12, 4, 11, 15, 0, 19, 6, 7, 8, -10};
int i = 0;
while (i < a.length)
{
if (a[i] > 10)
{
a[i]++;
}
}
[/cpp]
Although I generally use for loops to run through an Array, but I'm sure you will learn about that later.
Put your code in [noparse][code][/code] or [cpp][/cpp][/noparse] (works well for Java, C# and other C-style languages besides C++ too) tags to preserve whitespace and such. You'll get more help if your OP is neatly formatted (remember to indent your code, too!).
[QUOTE=Zyx;27394452]I never knew you could make an array like this:
int [] a = {12, 4, 11, 15, 0, 19, 6, 7, 8, -10};
Might become useful.
This code does that:
[cpp]
int[] a = {12, 4, 11, 15, 0, 19, 6, 7, 8, -10};
int i = 0;
while (i < a.length)
{
if (a[i] > 10)
{
a[i]++;
}
}
[/cpp]
Although I generally use for loops to run through an Array, but I'm sure you will learn about that later.[/QUOTE]
Ah I see what you mean, so what exactly does that do then? The one I pasted.
[editline]13th January 2011[/editline]
[QUOTE=jA_cOp;27394657]Put your code in [noparse][code][/code] or [cpp][/cpp][/noparse] (works well for Java, C# and other C-style languages besides C++ too) tags to preserve whitespace and such. You'll get more help if your OP is neatly formatted (remember to indent your code, too!).[/QUOTE]
Oh thanks very much man!
[QUOTE=Over-Run;27394796]Ah I see what you mean, so what exactly does that do then? The one I pasted[/QUOTE]
From what I can see it's missing a line or two so I will pretent it looks like this:
[cpp]
int [] a = {12, 4, 11, 15, 0, 19, 6, 7, 8, -10};
int i = 0;
while(i < a.length)
}
a[i] = a[i] + 1;
if(a[i] > 10)
}
i++;
{
}
[/cpp]
1: If i is less than the length of the array (which is 10) continue.
2: It starts by looking at index 0 of the array (Index 0 is the number 12 and it starts with 0 because the integer i = 0)
3: the number on index i is increased by one.
4: If the number on index i > 10 then i is increased by one.
5: go to 1:
Hopefully you understand, as I may be a bit bad to explain stuff like this.
[QUOTE=Zyx;27395030]From what I can see it's missing a line or two so I will pretent it looks like this:
[cpp]
int [] a = {12, 4, 11, 15, 0, 19, 6, 7, 8, -10};
int i = 0;
while(i < a.length)
}
a[i] = a[i] + 1;
if(a[i] > 10)
}
i++;
{
}
[/cpp]
1: If i is less than the length of the array (which is 10) continue.
2: It starts by looking at index 0 of the array (Index 0 is the number 12 and it starts with 0 because the integer i = 0)
3: the number on index i is increased by one.
4: If the number on index i > 10 then i is increased by one.
5: go to 1:
Hopefully you understand, as I may be a bit bad to explain stuff like this.[/QUOTE]
Yeah man I think I understand that, so It will keep going through the loop until all numbers are equal to 10, and if it is > than 10 it will add 1 to it!?
[QUOTE=Over-Run;27395336]Yeah man I think I understand that, so It will keep going through the loop until all numbers are equal to 10, and if it is > than 10 it will add 1 to it!?[/QUOTE]
Not exactly. It will keep going through the loop until i is 10 or greater.
i will only be increased if the number on index i is also 10 or greater.
So in a sense it will make all the numbers at least 11.
Oh I get you now thats why there was so many 11's thanks man.
I have a question(s) from Secion B, which is worth 60% of the exam so they are quite important.
By the way, these questions are all from old sample exams of course ha ha.
[quote]The following methods are intended to be part of a program to detect spam email. In the questions below,
you may assume that a word will comprise one or more lowercase letters (a-z) and that a sentence is a
sequence of words terminated by a full stop. Words are separated from each other by a single space
character.
a) Write a method that determines whether a lowercase letter is a vowel or not. The method should
take a character as a parameter and return a boolean value true if the character is a vowel and false
otherwise.
[6 Marks]
b) Write a method that will count the number of vowels in a word. The method will take a String
representing a word and return an integer, the number of vowels in the word.
[6 Marks]
c) Write a method which will take a word as a parameter and return the fraction of vowels in the
word. For example, in a four letter word with one vowel the ratio is 0.25. This method should
return a number of type double.
[5 Marks]
d) A ‘spam’ word is defined as one in which the fraction of vowels is outside the range 0.01 to 0.9.
Write a boolean method which takes a word as a parameter and determines if the word is a spam
word or not.
[5 Marks]
e) Write a method which takes a sentence as a parameter and counts the number of spam words in the
sentence. The method should return this count as an integer. [/quote]
[QUOTE=Over-Run;27398661]Oh I get you now thats why there was so many 11's thanks man.
I have a question(s) from Secion B, which is worth 60% of the exam so they are quite important.
By the way, these questions are all from old sample exams of course ha ha.[/QUOTE]
Are you expecting us to write that code for you or just give you some guidelines?
The point of these exams is to write these without outside help.
[QUOTE=TerabyteS;27390421]Print a "\n" before printing the "+ +"s[/QUOTE]
It looks like he's using C#. So I'm assuming he's using Windows. So "\r\n" is prefered.
[QUOTE=MulleDK13;27405021]It looks like he's using C#. So I'm assuming he's using Windows. So "\r\n" is prefered.[/QUOTE]
Are you retarded, he said this is done in java.
[QUOTE=MulleDK13;27405021]It looks like he's using C#. So I'm assuming he's using Windows. So "\r\n" is prefered.[/QUOTE]
Even in that case only \n would be needed.
[QUOTE=MulleDK13;27405021]It looks like he's using C#. So I'm assuming he's using Windows. So "\r\n" is prefered.[/QUOTE]
In C# you'd use System.Environment.NewLine.
In Java, which - as mentioned - the OP uses, java.lang.System.getProperty("line.separator");.
[QUOTE=ZeekyHBomb;27406096]In C# you'd use System.Environment.NewLine[/QUOTE]
\n is defined to be the same thing as Environment.NewLine
[highlight](User was permabanned for this post ("Alt of perabanned user." - cosmic duck))[/highlight]
[QUOTE=Combino;27406773]\n is defined to be the same thing as Environment.NewLine[/QUOTE]
[quote=http://msdn.microsoft.com/en-us/library/system.environment.newline.aspx]A string containing "\r\n" for non-Unix platforms, or a string containing "\n" for Unix platforms.[/quote]
[QUOTE=neos300;27400403]Are you expecting us to write that code for you or just give you some guidelines?
The point of these exams is to write these without outside help.[/QUOTE]
No no just looking for guidelines and help, I sort of find it hard to understand what it means sometimes. Sometimes you need outside help or you could be doing it entirely wrong.
[QUOTE=Over-Run;27398661]Oh I get you now thats why there was so many 11's thanks man.
I have a question(s) from Secion B, which is worth 60% of the exam so they are quite important.
By the way, these questions are all from old sample exams of course ha ha.[/QUOTE]
a) Seriously? What's your problem with checking against a few constants?
b) What's the problem here? You know a for-loop, right?
c) Well... is there anything further required than a division (in conjunction with the function from b))?
d) ...
e) String.split, Character.toLowerCase; 'nuff said.
Well.. I can understand that this might require a few pointers for a programming new-be, but you're like taking your exam seemingly soonish and I remember you having made a similar thread some time ago.
So, either you have a shit teacher, in which case you might wanna get a Java book or something, you need bigger support for learning in general, in which case the book is also a good option or perhaps ask someone good in your class if (s)he could go through the stuff with you, or programming is not well suited to you.
Maybe those exercises also just appear really easy to me, but would be hard for anyone in your class, but they just appear so fucking easy I find that hard to believe.
[QUOTE=Over-Run;27408091]No no just looking for guidelines and help, I sort of find it hard to understand what it means sometimes. Sometimes you need outside help or you could be doing it entirely wrong.[/QUOTE]
I'd say you should try and experiment with it yourself first. Make some new programs to do some interesting things you come up with (nothing too special) and try to guess what you should use to do what. That's the way to go. Asking here will grant that you forget all the things we said as soon as you get out of your room.
Those exercises really are easy for an exam.. Something that you would do a week or two after learning the language.. Are you in college?
[QUOTE=ZeekyHBomb;27408510]a) Seriously? What's your problem with checking against a few constants?
b) What's the problem here? You know a for-loop, right?
c) Well... is there anything further required than a division (in conjunction with the function from b))?
d) ...
e) String.split, Character.toLowerCase; 'nuff said.
Well.. I can understand that this might require a few pointers for a programming new-be, but you're like taking your exam seemingly soonish and I remember you having made a similar thread some time ago.
So, either you have a shit teacher, in which case you might wanna get a Java book or something, you need bigger support for learning in general, in which case the book is also a good option or perhaps ask someone good in your class if (s)he could go through the stuff with you, or programming is not well suited to you.
Maybe those exercises also just appear really easy to me, but would be hard for anyone in your class, but they just appear so fucking easy I find that hard to believe.[/QUOTE]
Thanks very much for the reply. The teacher is good its just I am not too good at programming. Do you have any books you would recommend? Specifically in Java?
[editline]15th January 2011[/editline]
[QUOTE=bunguer;27412858]Those exercises really are easy for an exam.. Something that you would do a week or two after learning the language.. Are you in college?[/QUOTE]
Well I don't know really its a Computer Applications degree so it isn't just programming we have to do other stuff like Web Design and Computer Hardware stuff, and a buisness module which I don't really understand. But yeah its in college, its a college called DCU and it got voted college of the year in Ireland so I presume its decent. We only do 2 hours of programming lectures a week so that might be it?
I also learned most Java stuff in school, we also had a book, but I don't remember its title or if it was good; I kinda just learned the stuff I needed as I've gone forward and nowadays I don't do Java programming, but help out mostly by just checking the official docs.
The Java-Standard is maintained and partially patented by Sun/Oracle and they have an [url=http://download.oracle.com/javase/tutorial/]official guide[/url], so I guess it's good.
Sorry, you need to Log In to post a reply to this thread.