Caporegime
no, else I would have typed that. It all depends on the needs of shicky, though yours will be closer to them.
I am also avoiding doing shicky's homework for him/her.
I am also avoiding doing shicky's homework for him/her.
I certainly appreciate that.no, else I would have typed that. It all depends on the needs of shicky, though yours will be closer to them.
I am also avoiding doing shicky's homework for him/her.
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; }
public static int enterMarks();
{
int[] classMarks = new int[30];
Dont quite understand there. I have this:
Code:public static [b]int[/b] enterMarks(); { [b]int[][/b] classMarks = new int[30]; ... return classMarks;
public static [b]int[/b] enterMarks();
public static [b]int[][/b] enterMarks();
public class marks2
{ // creates the array which the user will enter 30 values between 0-20 into
public static int[] enterMarks();
{
int[] classMarks = new int[30];
int a = 0;
for(int i = 0; i < classMarks.length;)
{
a = getScannerInput.anInt("Please enter a mark: ");
if (a >= 0 && a <= 20)
{
classMarks[i] = a;
i++;
}
else
{
System.out.println("The number you entered was not between 1 and 20");
}
}
for (int i = 0; i < classMarks.length; i++)
{
if (i < classMarks.length - 1)
{
System.out.print(classMarks[i] + ", ");
}
else
{
System.out.print(classMarks[i]);
}
return classMarks;
}
public static void main (String args[])
{
enterMarks();
}
for(int i = 0; i < classMarks.length; i++)
{
}
public class marks2
{ // creates the array which the user will enter 30 values between 0-20 into
public static void enterMarks();
{
int[] classMarks = new int[30];
int a = 0;
for(int i = 0; i < classMarks.length; i++;)
{
a = getScannerInput.anInt("Please enter a mark: ");
if (a >= 0 && a <= 20)
{
classMarks[i] = a;
}
else
{
System.out.println("The number you entered was not between 1 and 20");
}
}
for (int i = 0; i < classMarks.length; i++)
{
if (i < classMarks.length - 1)
{
System.out.print(classMarks[i] + ", ");
}
else
{
System.out.print(classMarks[i]);
}
}
}
}
for (int i = 0; i < something; i++)
{
}
int x = 0;
while (x < something)
{
//do something
x++;
}
int i= 0;
while(i < classMarks.length)
{
a = getScannerInput.anInt("Please enter a mark: ");
if (a >= 0 && a <= 20)
{
//valid input - record and move on to the next mark
classMarks[i] = a;
i++;
}
else
{
//invalid input - ask again
System.out.println("The number you entered was not between 1 and 20");
}
}
public class Marks
private ArrayList marksArray;
public static void main (String[] args) {
Marks marks = new Marks();
marks.collectMarks();
marks.printMarks();
}
public void collectMarks () {
int i = 0;
String input = '';
while (i < 30) {
input = System.in.readLine();
if (input > 0 && input < 21) {
i++;
this.marksArray.add(input);
} else {
System.out.printLn('Please enter a number between 1 and 20.');
}
}
}
public void printMarks () {
// depending on desired formatting, either this for simplicity:
System.out.print(this.marksArray.toString());
// or
Iterator it = marksArray.iterator();
Boolean sep = false;
while (it.hasNext()) {
if (sep) {
System.out.print(',');
} else {
sep = true;
}
System.out.print(' ' + it.next().toString());
}
}
}
Shicky, you are right but a while loop would be a better fit.
SiriusB, we only want to increment "i" when valid input is entered.
Shicky, if you're unsure of the "this" keyword used above and in the class constructors of Pr0t0c0L's example earlier in the thread have a look here:This is why I suggested using ArrayList.
Having to call the interface of a parent class/Interface increases memory usage when it is not necessary, over calling the specific interface of the object you will be instantiating.
public interface Test {
void test();
}
public class TestImpl implements Test {
public void test()
{
System.out.println("test") ;
}
}
public class Main {
public static void main(String[] args){
Test referenceToTest = new TestImpl();
TestImpl referenceToTestImpl = new TestImpl();
//Call 1
referenceToTest.test();
//Call 2
referenceToTestImpl.test();
}
}
Walking before running is fine, but even students are expected to learn how to put one foot infront of the other by themselves. This is somehing especially necessary in development.. you don't get a textbook telling you how to create code for your customers needs.. you, shock horror I know, need to come up with that design yourself.
Hint: You're calling upon two objects when not referencing the same interface.Bull.
Code:public interface Test { void test(); }
Code:public class TestImpl implements Test { public void test() { System.out.println("test") ; } }
Code:public class Main { public static void main(String[] args){ Test referenceToTest = new TestImpl(); TestImpl referenceToTestImpl = new TestImpl(); //Call 1 referenceToTest.test(); //Call 2 referenceToTestImpl.test(); } }
Are you really saying that Call 1 has a greater memory overhead than call 2?
(Hint: compile the above example and profile it.....)