Seriously screwed for Java assessment

Associate
Joined
13 Jan 2007
Posts
2,424
Location
Belfast,Northern Ireland
Got an assessed practical tomorrow, im normally pretty pants at Java but to add to things i've deleted my practical work from the last 2 sessions on arrays and now really am in the dark. Is there any site at all java programmers can use for knowledge? Everything i've found so far has been extremely unhelpful, including sun microsystems own stuff
 
Associate
OP
Joined
13 Jan 2007
Posts
2,424
Location
Belfast,Northern Ireland
I worked through a bit of it but found it was mainly definations/explainations or overly simplified examples like 'Helloworld.' Guess i'll crack on through it once i get some time.

Its really simple problems, i've lost all my files and cant get on my unis net storage, which i find not so great!

I just want to create an array which the user enters 30 elements into (through getScannerInput) between the numbers 0-20 which are ordered as they were entered. Then print these numbers in order seperated by a comma and a space e.g. (1, 3, 6, 19)

I know its really basic but its been one of those days to say the least. I know how to restrict numbers okay but does this change for arrays somehow as it could mess up entry order?
 
Associate
Joined
2 Dec 2004
Posts
314
Location
Parallel Dimension
What is getScannerInput? I type it into google and I get a bunch of things that seem to be exactly what you're looking for.

Is it specific to your uni or something? Arrays are easy.

Code:
int[] marks = { 10, 20, 11, 15, 5, 2, 16 }

for (int i = 0; i < marks.length; i++)
{
  if (i < marks.length - 1)
  {
    System.out.print(marks[i] + ", ");
  }
  else
  {
    System.out.print(marks[i]);
  }
}
The if statement inside the loop checks to see if the element isn't the last in the array. If it is the last element then it doesn't tag a comma on the end.
 
Associate
OP
Joined
13 Jan 2007
Posts
2,424
Location
Belfast,Northern Ireland
i think i love you magical trevor!

getScannerInput is a .class written by my lecturer or something in my uni which lets you ask the user to input the data basically.

So for example: int a = getScannerInput.anInt("Enter first number: ");

Allows the user to enter the number. This has just added to confusion as it functions differently from everything i can find on the web so i cant make it work for this array.

What makes it worse is i did it last week but dont have my old files for reference :(
 
Associate
Joined
2 Dec 2004
Posts
314
Location
Parallel Dimension
This should probably work. Been a while since I've done java... syntax is the same as C# though so shouldn't be too far from the truth.

Code:
// x is the number of marks to get
private int[] getMarks(int x)
{
  int[] marks = new int[x];
  
  for (int i = 0; i < marks.length; )
  {
    int a = getScannerInput.anInt("Enter a number: ");

    if (a > 0 && a < 20)
    {
      marks[i] = a;
      i++;
    }
    else
    {
      System.out.println("The number you entered was not between 1 and 20");
    }
  }
}

// marks is the array you are going to print out
private void printMarks(int[] marks)
{
  for (int i = 0; i < marks.length; i++)
  {
    if (i < marks.length - 1)
    {
      System.out.print(marks[i] + ", ");
    }
    else
    {
      System.out.print(marks[i]);
    }
  }
}

public void run()
{
  printMarks(getMarks(30));
}

Have you been taught about public and private methods and such yet? If you need an explanation about whats going on let me know.
 
Associate
OP
Joined
13 Jan 2007
Posts
2,424
Location
Belfast,Northern Ireland
Not really, teachings been of a fairly poor standard but to be fair i should be doing a lot more private study than what i have been. But i have been doing this for the other 2 modules as they've been very heavy workloads of late.

Cheers for the code, i'd just solved it myself actually but i hadnt stuck the restrictions in yet and yours is much neater.

Can i ask what this is ? Just a comment?
// x is the number of marks to get
private int[] getMarks(int x)
 
Associate
Joined
2 Dec 2004
Posts
314
Location
Parallel Dimension
Not really, teachings been of a fairly poor standard but to be fair i should be doing a lot more private study than what i have been. But i have been doing this for the other 2 modules as they've been very heavy workloads of late.

Cheers for the code, i'd just solved it myself actually but i hadnt stuck the restrictions in yet and yours is much neater.

Can i ask what this is ? Just a comment?
// x is the number of marks to get
private int[] getMarks(int x)

Yeah comments are // for single line and /* (opening) */ (closing) for multiple line comments.

Neatness of code and comments always helps you understand what is going on... especially when you've finished whatever the hell it was you were programming... notice a bug and they you're looking at a method and thinking what the hell is going on here. Because you've forgotten how you solved it.

Just noticed too in my solution you probably want to change the checking to.

Code:
if (a >= 0 && a <= 20)
 
Last edited:
Associate
Joined
3 Dec 2003
Posts
943
Its better when you solve specific problems like this to open up the program so it works under different constraints. so you might want to take 10 numbers between 1 and 5 without big code change. below is an example expanding the problem domain to make the program more flexible.
This approach will be useful as you continue to learn java and code larger programs

Code:
import java.util.Scanner;

public class ArrayReader {
	
	private int[] numbers;
	private int readLimit;
	private int intMin;
	private int intMax;
	
	public ArrayReader() {
		this(30,0,20);
	}
	public ArrayReader(int readLimit,int intMin, int intMax) {
		this.readLimit = readLimit;
		this.intMin = intMin;
		this.intMax = intMax;
		numbers = new int[readLimit];
	}	
	public String read(){
		 int count = 0;
		 Scanner in = new Scanner(System.in);
		 System.out.println("Please enter "+readLimit +" numbers between "+intMin + " and "+intMax);
		 while(count<readLimit) {
			 int number = in.nextInt();
			 if( ( number >= intMin ) && ( number <= intMax ) ) {
				 numbers[count] = number;
				 count++;			 
			 }
			 else {
				 System.out.println("Please enter numbers between "+intMin + " and "+intMax);				 
			 }
		 }
		 return this.toString();
	}
	public String toString(){
		String str = "";
		for( int i = 0; i< numbers.length;i++){
			if(i != 0) {
				str+= ", ";
			}
			str+= numbers[i];
		}
		return str;
	}
	public static void main(String[] args) {
		//example of 10 numbers between 1 and 5
		//ArrayReader arrayReader = new ArrayReader(10,1,5);
		
		//Defaults to 30 numbers between 0  and 20 with empty contructor
		ArrayReader arrayReader = new ArrayReader();		
		System.out.println(arrayReader.read());
	}
}
 
Associate
OP
Joined
13 Jan 2007
Posts
2,424
Location
Belfast,Northern Ireland
Cant help but gulp at this stuff!

Must say i really do feel almost cheated. Though i can slack i am generally a pretty good student in terms of actually working hard and attending all my lectures etc. But despite this my grasp of java is turd so far, lectures seem to be a 50minute session of the lecture trying to make ppl fall asleep his hardest. And though i pay attention, a 15minute recap on what we did last lecture, followed by new things which explained once, hazzah i understand, explained another 7 times getting dumber as he goes just serves to bore me.

Then practicals which were going fine till about 3 weeks ago when suddenly we were meant to pull solutions out of our rear ends to things we'd never seen before. As for our textbook, biggest waste of 20quid ever. It in no way delivers any useful information as it only just exceeds about 20 bloody pages. Upon asking the lecturer why we didnt have a better, more detailed book, he said it was too confusing for those performing not so well in the class.......the rest i would have to bleep out.

Not impressed by the standard of teaching or any help im getting at all

/rant over

*Forgot to say thanks to you guys for the help, i was dead in the water there without my old files for reference, at least tomorrow i'll go in there with some grasp of what im doing
 
Soldato
Joined
9 May 2005
Posts
4,524
Location
Nottingham
And I found the JDK extremely helpful when doing Java.

I am assuming he means the Java API Specification which can be found at http://java.sun.com/javase/6/docs/api/index.html

He's correct, it's probably the most useful reference any programmer can have when writing Java as it lists all of the built in classes and methods. For example the Java.util package contains a class called Arrays which you can see contains many methods, one being sort. Sort can be used to sort an array into ascending order and can work on arrays of many different types. Java.util.Arrays.sort(int[] a) can be used to sort an array of integers into ascending order.
 
Soldato
Joined
16 Dec 2005
Posts
14,443
Location
Manchester
Cant help but gulp at this stuff!

Must say i really do feel almost cheated. Though i can slack i am generally a pretty good student in terms of actually working hard and attending all my lectures etc. But despite this my grasp of java is turd so far, lectures seem to be a 50minute session of the lecture trying to make ppl fall asleep his hardest. And though i pay attention, a 15minute recap on what we did last lecture, followed by new things which explained once, hazzah i understand, explained another 7 times getting dumber as he goes just serves to bore me.

Then practicals which were going fine till about 3 weeks ago when suddenly we were meant to pull solutions out of our rear ends to things we'd never seen before. As for our textbook, biggest waste of 20quid ever. It in no way delivers any useful information as it only just exceeds about 20 bloody pages. Upon asking the lecturer why we didnt have a better, more detailed book, he said it was too confusing for those performing not so well in the class.......the rest i would have to bleep out.

Not impressed by the standard of teaching or any help im getting at all

/rant over

*Forgot to say thanks to you guys for the help, i was dead in the water there without my old files for reference, at least tomorrow i'll go in there with some grasp of what im doing

When I did Java at University our lecturer treated everyone like a complete imbecile and would pretty much over-explain everything. He was the leading candidate for the Master of the Obvious award. If you were one of the "dim" students [of which there were plenty] then great, it must have been a great help to them. If you were fortunate enough to understand the difference between a method and a class it was literally a lesson in tedium at times.

[As an aside, despite this teacher's effort to teach even the lowliest creature in our class, I did overhear, on the day of a Java exam, one piece of spewtum ask "so how does if work again?". There is just no helping some people...]

To be honest, programming can be hard for a lot of people to get their head around, regardless of how good/bad the teaching. Especially OO programming as it far more strict and disciplined, and has quite a few concepts that you have to wrap the grey matter around. There were times when I would be shown something and go "err...?" and scratch my head. Then suddenly, out of the blue, it would drop into place.

The absolute best way of shoehorning things into place is to do lots of practice and lots of tinkering. I would also get a better book!

One I recommend is book called Objects First with Java. http://www.bluej.org/objects-first/

This was the text book we used and I think it is fantastic. It keeps things simple when introducing new concepts and does a great job of showing you how it works. The examples and practice questions are all very good too. The book even comes with a Java IDE called BlueJ, which is nice. It is a very simple environment, but I think it is invaluable to beginners. Once you are more confident, you can move on to more complex/better IDEs.

If you are willing to put the effort in then you will soon get the hang of it. Once you do, you can apply that knowledge to plenty of other languages as concepts and structures are generally the same in any OO language.
 
Associate
OP
Joined
13 Jan 2007
Posts
2,424
Location
Belfast,Northern Ireland
cheers guys, that sorting of the array isnt a problem mate, sirusb and magical trev sorted me out.

As for that BlueJ book its already on order, think yourself and some other guys recommended it in my last java thread. How long do you continue to use java for? Throughout my time at uni or do we switch languages?

I was wondering how to make a method that will display the most frequently occurring mark and display how many people got it? From the array of course. I've had to do max's, min's, averages, and displayed how many passed in terms of true/false
 
Associate
OP
Joined
13 Jan 2007
Posts
2,424
Location
Belfast,Northern Ireland
urgh i changed it so it would go inside a method and now its completely ballsed when i try to return the array so i can use it for other stuff in the main method. I HATE JAVA! killing rampage coming soon
 
Associate
Joined
17 Oct 2002
Posts
2,165
Location
London
return the variable, and make sure your method signature contains the class name of the object you are returning.
Code:
public Array foo () {
    Array foobar = new Array();
    return foobar;
}
Don't you mean
Code:
//returns an array of integers
public int[] getArray() {
    //create a new array
    int[] foobar= new int[20];

    return foobar;
}
 
Back
Top Bottom