please help in java program

Status
Not open for further replies.

shivi4

Journeyman
Problem 4
DO NOT USE ANY LOOPS,IF ELSE OR SWITCH CASE
Write a program that transforms numbers 1, 2, 3, 4, ....12 into the corresponding month names January, .........
For example, input is 5
Output is as follows:
Month name corresponding to 5 is May.

You will be writing a class MonthNames with a method for returning month name and any other thing required.
You also need to write a class TestMonthNames for taking input from user, creating object, calling methods and for output as shown above.


PLEASE CAN YOU GIVE CONCEPT OF HOW TO SOLVE ABOVE PROB
DO NOT USE ANY LOOPS,IF ELSE OR SWITCH CASE
 

siriusb

Cyborg Agent
Code:
string GetMonth(int idx)
{
   string months[]={"January", "february","march","april","may","june","july","august","september","october","november","december"};
   return (months[idx]);
}
Put this in a class, format the output and submit the assignment.
 

siriusb

Cyborg Agent
import java.text.DateFormatSymbols
...
string GetMonth(int idx)
{
return (getMonths()[idx]);
}
...


It's more like a logic than actual code. See if that works.
 
OP
S

shivi4

Journeyman
WE HAVE TO USE SIMPLE LOGIC
NO EXTERNAL CLASSES NO ARRAYS NO LOOPS IF/ELSE

NO SWITCH OR BREAK

ONLY CLASSES AND OBJECT S AND CONSTRUCTOR

DEFAULT DATA TYPE
 

QwertyManiac

Commander in Chief
This one shows a lot of output but the correct one is a complete word...
Code:
import java.io.*;
class Month
{
	public static void main(String args[]) throws IOException
	{
		String A;
		DataInputStream in = new DataInputStream(System.in);
		A=in.readLine();
		System.out.println(A.replaceAll("1","January"));
		System.out.println(A.replaceAll("2","February"));
		System.out.println(A.replaceAll("3","March"));
		System.out.println(A.replaceAll("4","April"));
		System.out.println(A.replaceAll("5","May"));
               // And So on....
	}
}
Save as Month.java
 
OP
S

shivi4

Journeyman
PLEASE CAN YOU GIVE CONCEPT OF HOW TO SOLVE ABOVE PROB
DO NOT USE ANY LOOPS,IF ELSE OR SWITCH CASE

NO ARRAY IS ALLOWED

we have in built string class in java


we have to its method only like indexof,substring etc
 

siriusb

Cyborg Agent
Well, he did use string clas and its methods. The datainputstream was only to get a kbd input. U can replace it with a function argument input.
 
Status
Not open for further replies.
Top Bottom