• Post Reply Bookmark Topic Watch Topic
  • New Topic

Art and Science of Java Chapter 4 Exercise 7 (non draft version)  RSS feed

 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey

I am green as green comes to both Java and Programming but trying to improve . I started with The Art and Science of Java book and the course CS106a programming Methodology at Stanford.I completed the exercise 7 at chapter 4 but i think i have user a bloted /innefective solution .Here is the exercise :

ex 7 page 128 (non draft version - for the draft version nr 7 is a lesser/easier exercise) :

7.The digital root of an integer n is defined as a result of summing the digits repeatedly until only a single digit remains. For example , the digital root of 1729 can be calculated by using the following steps :

Step 1 : 1 + 7 + 2 + 9 => 19
Step 2 : 1 + 9 => 10
Step 3 : 1 + 0 => 1

Rewrite the DigitSum program so that it calculates the digital root of the input value .

-------------------------------------------------------------------------------------------------------------------------------------

The DigitSum looks like this :




My own solution for this exercise is :





Not using strings / characters or any more advanced features of Java can you tell me how i can write it more efficient ? Using only the tools i learned so far aka if /for /while /break and using the DigitSum as a starting model/template?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tiberius Marius wrote:Not using strings / characters or any more advanced features of Java can you tell me how i can write it more efficient ? Using only the tools i learned so far aka if /for /while /break and using the DigitSum as a starting model/template?

Well:

1. Efficiency should NOT be your concern at the moment. Indeed, it rarely should be. If you don't believe me, look here (my favourites are #'s 1, 2, 6, 7 and 10).
2. What about making the "digit sum" part of your calculation a method? Eg:
public static final int sumOfDigits(int n) { ...
then you could just keep calling it while the result is >= 10.
3. (a bit more advanced) Java allows methods to call themselves. It's called 'recursion'. Now it almost certainly won't make your solution more efficient, but it may well make it very compact.

Winston
 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand what you say but i still think i have stuff to learn if i look on others people way of solving problems ,especially if they do a better job then me.Also the reason i don't want to use methods/recursively is because i haven't got to that chapter yet /don't know how they work (except basic understanding).If you have any suggestions or exercises that can help me at my current level i m all ears .
 
Scott Winterbourne
Ranch Hand
Posts: 116
2
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tiberius Marius wrote:I am green as green comes to both Java and Programming but trying to improve . I started with The Art and Science of Java book and the course CS106a programming Methodology at Stanford.I completed the exercise 7 at chapter 4 but i think i have user a bloted /innefective solution .

Is the Stanford class with Mehran Sahami? He's a good professor.
 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scott Winterbourne wrote:
Tiberius Marius wrote:I am green as green comes to both Java and Programming but trying to improve . I started with The Art and Science of Java book and the course CS106a programming Methodology at Stanford.I completed the exercise 7 at chapter 4 but i think i have user a bloted /innefective solution .

Is the Stanford class with Mehran Sahami? He's a good professor.


Yes
 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have another exercise i suspect can be solved more efficiently :

Art and Science of Java Chapter 4 Exercise 14 , basically i input values until i input a stop value ( here santinel) The program should display the highest of the values inputted and the second highest ( the trickier part) .
This is how i resolved after some tries :

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tiberius Marius wrote:I understand what you say but i still think i have stuff to learn if i look on others people way of solving problems ,especially if they do a better job then me.Also the reason i don't want to use methods/recursively is because i haven't got to that chapter yet /don't know how they work (except basic understanding).If you have any suggestions or exercises that can help me at my current level i m all ears .

"Better" is a qualitative adjective that assumes that YOU know what is 'better' and what is 'worse'. I've been at this business for 37 years, and in many (admittedly, more complex) areas, I don't know. So, right now, stick with "best that you know how". If your "best" isn't the best, your tutors will tell you.

But none of this answers your question: "can you tell me how i can write it more efficient". I already tried to answer; but, in pure terms, and if you want me to answer as an examiner:
I see nothing wrong with the basic code, but that's all it is. Code. There is nothing (eg. a function or method) for anyone else to take from it.
and on that basis I'd give it 40% (simply for being right).

Winston

Edit: I should add that this post was before OP's new topic.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
are you sure that works?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tiberius Marius wrote:I have another exercise i suspect can be solved more efficiently :
Art and Science of Java Chapter 4 Exercise 14 , basically i input values until i input a stop value ( here santinel) The program should display the highest of the values inputted and the second highest ( the trickier part) .
This is how i resolved after some tries :

Question: Have you tried it, where ALL your input values are negative numbers < -1?

What happens?

Winston
 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I does not work with negative numbers and that s fine , the exercise specifies that it should work only for positive numbers .
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tiberius Marius wrote:I does not work with negative numbers and that s fine , the exercise specifies that it should work only for positive numbers .

Fair enough. It still might be worth trying to figure out why it doesn't work for negative numbers, because it's extremely simple to write a version that will work for ANY number.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!