473,386 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

New to Java with a Static Method Question

I am new to java and I am trying to learn how to to reduce lines of code that are redundant in a simple file I made. I am trying to figure out how to reduce this code down to 13 methods not including the main and reduce any extra lines without using array's



Expand|Select|Wrap|Line Numbers
  1. public class Song {
  2.     public static void main(String[] args) {
  3.             One();
  4.         Two();
  5.         Three();
  6.         Four();
  7.         Five();
  8.         Six();
  9.         Seven();
  10.       }
  11.        public static void One() {
  12.             System.out.println("On the 1st day of \"Xmas\", my true love sent to me");
  13.         OneVerse();
  14.       }
  15.        public static void Two() {
  16.               System.out.println("");    
  17.               System.out.println("On the 2nd day of \"Xmas\", my true love sent to me");
  18.         TwoVerse();
  19.         OneVerse();
  20.       }
  21.        public static void Three() {
  22.             System.out.println("");    
  23.               System.out.println("On the 3rd day of \"Xmas\", my true love sent to me");
  24.         ThreeVerse();
  25.         TwoVerse();
  26.         OneVerse();
  27.       }
  28.        public static void Four() {
  29.             System.out.println("");    
  30.               System.out.println("On the 4th day of \"Xmas\", my true love sent to me");
  31.         FourVerse();
  32.         ThreeVerse();
  33.         TwoVerse();
  34.         OneVerse();
  35.       }
  36.        public static void Five() {
  37.             System.out.println("");    
  38.               System.out.println("On the 5th day of \"Xmas\", my true love sent to me");
  39.             FiveVerse();
  40.         FourVerse();
  41.         ThreeVerse();
  42.         TwoVerse();
  43.         OneVerse();
  44.       }
  45.        public static void Six() {
  46.             System.out.println("");    
  47.               System.out.println("On the 6th day of \"Xmas\", my true love sent to me");
  48.        SixVerse();    
  49.         FiveVerse();
  50.         FourVerse();
  51.         ThreeVerse();
  52.         TwoVerse();
  53.         OneVerse();
  54.       }
  55.        public static void Seven() {
  56.             System.out.println("On the 7th day of \"Xmas\", my true love sent to me");
  57.         SevenVerse();
  58.         SixVerse();    
  59.         FiveVerse();
  60.         FourVerse();
  61.         ThreeVerse();
  62.         TwoVerse();
  63.         OneVerse();        
  64.       }
  65.        public static void OneVerse() {
  66.               System.out.println("a partridge in a pear tree.");
  67.       }
  68.         public static void TwoVerse() {
  69.             System.out.println("two turtle doves, and");        
  70.       }
  71.         public static void ThreeVerse() {
  72.             System.out.println("three French hens,");
  73.       }
  74.         public static void FourVerse() {
  75.             System.out.println("four calling birds,");
  76.       }    
  77.         public static void FiveVerse() {
  78.             System.out.println("five golden rings,");    
  79.       }
  80.         public static void SixVerse() {
  81.             System.out.println("six geese a-laying,");
  82.       }
  83.         public static void SevenVerse() {
  84.             System.out.println("Seven swans a-swimming,");    
  85.       }
  86.   }
Jan 14 '08 #1
7 3110
Shashi Sadasivan
1,435 Expert 1GB
Hi,
this is the .Net forums, I will move the thread to the java forum. Please look at where you post your thread to get more responses.

Regarding this question, Can you store the strings into an array. and make a method which will print the first n strings in that array. this way you can reduce it much lesser than 13 methods.

-Shashi
Jan 14 '08 #2
Hi,
this is the .Net forums, I will move the thread to the java forum. Please look at where you post your thread to get more responses.

Regarding this question, Can you store the strings into an array. and make a method which will print the first n strings in that array. this way you can reduce it much lesser than 13 methods.

-Shashi
Thanks for moving it. I haven't learned array's yet but was told I could use call methods and get it down to 13 methods not including the main just not sure what to change.
Jan 14 '08 #3
Laharl
849 Expert 512MB
If you set up a static array of strings with the song verses in it, and use an integer parameter for which verse you need in a sing() function that calls itself recursively, you can do this in 2 functions, main included.

If by 13 methods, your instructor means 13 calls, this might cover it, I'm not sure. You'd have to track the recursive calls.
Jan 14 '08 #4
Ganon11
3,652 Expert 2GB
You could also do this using a tricksy switch statement. You would pass an integer dayNum to the method, and use this number to print the whole list of gifts. You'll need to decide what order you make your switch statements, what you have to do before the switch statement (if anything), and any special cases you'll have to take care of.
Jan 14 '08 #5
BigDaddyLH
1,216 Expert 1GB
You could also do this using a tricksy switch statement. You would pass an integer dayNum to the method, and use this number to print the whole list of gifts. You'll need to decide what order you make your switch statements, what you have to do before the switch statement (if anything), and any special cases you'll have to take care of.
"The Twelve Days of Christmas" is the standard example of that hack. Outside of that, it's rarely used.
Jan 14 '08 #6
Ganon11
3,652 Expert 2GB
"The Twelve Days of Christmas" is the standard example of that hack. Outside of that, it's rarely used.
Yeah, I can't think of very many other practical applications of omitting the break; statement.
Jan 14 '08 #7
JosAH
11,448 Expert 8TB
Yeah, I can't think of very many other practical applications of omitting the break; statement.
Not in Java but in C it was/is a very practical way of manual loop unrolling.
See Duff's device for a nice example.

kind regards,

Jos
Jan 14 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

73
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities...
2
by: Andrea | last post by:
Hi evrybody ! Does anybody know how to call a java class from a COM component ? Any suggestion ? Thanks Andrea
2
by: Michael | last post by:
Running DB2 v7 UDB ("DB2 v7.1.0.93", "n031208" and "WR21333") on Windows XP, I am unable to find out why the "Build for Debug" option within Stored Procedure Builder is not enabled on Java stored...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
6
by: Rhino | last post by:
I'm trying to debug a simple Java UDF written in the DB2General style within Eclipse. I'm getting a java.lang.UnsatisfiedLinkError when I execute the set() method in the UDF. I know that the...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
5
by: r035198x | last post by:
Setting up. Getting started To get started with java, one must download and install a version of Sun's JDK (Java Development Kit). The newest release at the time of writting this article is...
5
by: Daniel | last post by:
Hi all, I have some questions regarding C#. I come from a Java background. I am using .NET 1.1. Please limit your answers to v 1.1, otherwise please state the version to which it applies to. ...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.