increment integer

I am a new student with Java and I need to increment an interger with

Page 1 of 1

5 Replies - 21997 Views - Last Post: 03 February 2011 - 11:31 AM Rate Topic: -----

#1 shirleystevenson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 16-September 08

increment integer

Post icon  Posted 16 September 2008 - 07:36 PM

//Increment an integer (that defaults to 22) by 1 then adds to integer (which the max value is 27)

int num = newInt(22);
int maxInt < 28;

int tot_Int;

num = newInt(num.intValue() + 1);

I am kind of lost at this point,what do I need to put in here so this program will stop incrementing at 27?

System.out.println(num);

Is This A Good Question/Topic? 0
  • +

Replies To: increment integer

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4361
  • View blog
  • Posts: 12,180
  • Joined: 18-April 07

Re: increment integer

Posted 16 September 2008 - 07:49 PM

whoa whoa here you are going the wrong direction. First of all, basic data types like int, float, double etc don't need to be initialized with the keyword "new". Next when you do use new, there is a space after it before the class name. It is not newClass it is new Class().

So when it says defaults to 22, it means it is looking for a declaration like int num = 22;

Next relational operators (like greater than, less than, equal to etc) return a value of boolean (aka true or false) so they are used in conditions.

What your assignment/instructor is looking for is for you to create a loop that increments a value (num) up to 27 stepping by 1. It looks like this...

for (int num = 22; num <= 27; num++) {
     System.out.println(num);
}



Here the for loop is saying create an integer value "num" to start at 22, go while it is less than or equal to 27 (notice this is a condition that returns true or false) and lastly at the end of each loop add 1 to the value.

So this loop will print out the numbers 22 through to 27 on the screen.

Hope this make sense. Please review looping and pay attention to the three parts of the for loop... the initializer, the condition, and the post operation.

Good luck

"At DIC we be for loop crazy code ninjas... we just be crazyyyyy!" :snap:
Was This Post Helpful? 1
  • +
  • -

#3 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: increment integer

Posted 17 September 2008 - 04:12 PM

A more object oriented approach could look like this:

public class myInt {
  private int number;

	public myInt() {
	  number = 22;
	}

	public myInt(int i) {
	  number = i;
	}

	public void incr() {
		number++;
	}

	public boolean maxInt() {
	  return (number >= 27);
	}

	public void printInt() {
	  System.out.println(number);
	}
}



Note the 2 constructors allowing you to have the start value assigned as default = 22 or defined by the programmer.

Then in your main class you could write:

myInt someint = new myInt(); // or myInt someint = new myInt(22);
someint.printInt();
while (!someint.maxInt()) {
  someint.incr();
  someint.printInt();
}



This is sort of the complicated way of doing what Martyr2 did in only 2 (3) lines. However, this might give you an idea what object oriented programming is about.
Was This Post Helpful? 1
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: increment integer

Posted 17 September 2008 - 05:29 PM

View PostGloin, on 17 Sep, 2008 - 04:12 PM, said:

A more object oriented approach could look like this:

public class myInt {
  private int number;

	public myInt() {
	  number = 22;
	}

	public myInt(int i) {
	  number = i;
	}

	public void incr() {
		number++;
	}

	public boolean maxInt() {
	  return (number >= 27);
	}

	public void printInt() {
	  System.out.println(number);
	}
}



Note the 2 constructors allowing you to have the start value assigned as default = 22 or defined by the programmer.

Then in your main class you could write:

myInt someint = new myInt(); // or myInt someint = new myInt(22);
someint.printInt();
while (!someint.maxInt()) {
  someint.incr();
  someint.printInt();
}



This is sort of the complicated way of doing what Martyr2 did in only 2 (3) lines. However, this might give you an idea what object oriented programming is about.


Feel like going back to SmallTalk era
SmallTalk had no primitive data type. Everyting was an object... and it was horribly slow
But for some purists it is still "the" OO language

This post has been edited by pbl: 17 September 2008 - 08:47 PM

Was This Post Helpful? 0
  • +
  • -

#5 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: increment integer

Posted 17 September 2008 - 05:32 PM

Slow is great, means time for more coffee.
Was This Post Helpful? 0
  • +
  • -

#6 PlexPro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-February 11

Re: increment integer

Posted 03 February 2011 - 11:31 AM

Hi,

That's a nice object.

Some preferences that I've developed over the years would lead me to make a few tweaks. I would replace maxInt() with inRange(), thusly:

	public boolean inRange() {
	  return (number < 28);
	}



Then I would implement the loop with a while-do primitive construct instead of a do-until construct:

myInt someint = new myInt();
while (someint.inRange()) {
  someint.printInt();
  someint.incr();
}



Thanks for your example!

PlexPro


View PostGloin, on 17 September 2008 - 04:12 PM, said:

A more object oriented approach could look like this:

public class myInt {
  private int number;

	public myInt() {
	  number = 22;
	}

	public myInt(int i) {
	  number = i;
	}

	public void incr() {
		number++;
	}

	public boolean maxInt() {
	  return (number >= 27);
	}

	public void printInt() {
	  System.out.println(number);
	}
}



Note the 2 constructors allowing you to have the start value assigned as default = 22 or defined by the programmer.

Then in your main class you could write:

myInt someint = new myInt(); // or myInt someint = new myInt(22);
someint.printInt();
while (!someint.maxInt()) {
  someint.incr();
  someint.printInt();
}



This is sort of the complicated way of doing what Martyr2 did in only 2 (3) lines. However, this might give you an idea what object oriented programming is about.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1