查看: 722|回复: 7
|
how to do this question?
[复制链接]
|
|
Roman numerals are used frequently to indicate dates, in particular the year (for example, MMVI represents the year 2006). There are two main differences between roman and arabic numerals. - There is no symbol for zero in roman numerals;
- Numeral placement in the roman system can indicate either addition or subtraction.
Your task for this lab assignment is to convert a number ranging from 1 to 9999 to its roman numeral equivalent. Roman Numeral ConversionThe conversion is specified using the following cases. - I represents 1. Joining two I's together (i.e. II) represents 2, while III represents 3. However, IIII to represent 4 is forbidden because there is a shorter notation.
- V represents 5. Placing I before V (i.e. IV) will mean subtract I from V, i.e. 4. Note that we do not subtract more than one I from V, so IIV to represent 3 is invalid. Placing I after V (i.e. VI) will mean add I to V, i.e. 6. Likewise, VII and VIII represents 7 and 8. To represent 9 and 10, we do not use VIIII and VV respectively because there exist shorter notations.
- X represents 10. Following the rule above, IX is 9 while XI is 11.
- L represents 50 and works in much the same way as V. So XL is 40 while LX is 60. Additionally, XLI is 41, XLV is 45, XLIV is 44, XLVI is 46, XLIX is 49. Hopefully you get the picture...
- C represents 100.
- D represents 500.
- M represents 1000. M is the last character of the roman numeral system so to represent 9000, there is no choice but to use MMMMMMMMM.
The TaskWrite a program roman.java that requests a year from the user. Assuming that the executable is roman, a sample run of the program is shown below. User input is denoted in bold. Enter the year : 1Year 1 in roman numerals is I. | Enter the year : 2006Year 2006 in roman numerals is MMVI. | Enter the year : 1998Year 1998 in roman numerals is MCMXCVIII. | Enter the year : 9999Year 9999 in roman numerals is MMMMMMMMMCMXCIX. | Enter the year : 9888Year 9888 in roman numerals is MMMMMMMMMDCCCLXXXVIII. | For simplicity, we assume that user input will always be within the range of permissible values, so there is no need to perform any explicit checks on the data entered by the user.
Task 2 - Clock WorkIn Lab 1, we have seen how turtle graphics can be used to draw the clock face. In this part of the assignment, you shall make your clock start ticking. Write a program clock.java that extends time.java such that the clock will begin ticking from the computed canonical time. The clock face should be updated after every one second has elasped. Points to Ponder...- As we would like our clock to run for as long as possible, will your program eventually fail to function properly due to overflow error? How would you prevent it from happening? Remember: Overflow prevention is your responsiblity...
- How would you expect your clock to perform in the long run? Would it be faster or slower?
You are to document your observations/findings to the above questions as part of the header comments of your code. Due to the lack of sample output, you might consider letting your discussion leader have a look at your clock before submitting the clock.java file, so as to ensure everything is in order.
*dis quetion can oni use if,while,do-while loop.cnt introduce array,string inside.Plz teach me.desperately need help.thank you. |
|
|
|
|
|
|
|
发表于 3-2-2007 01:00 PM
|
显示全部楼层
嘿嘿。。。别把整题assignment问题 post 上来呀。
别人看到了这样长的问题会变得不想看下去。
我就是。。。 |
|
|
|
|
|
|
|
发表于 3-2-2007 02:16 PM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 3-2-2007 04:22 PM
|
显示全部楼层
|
|
|
|
|
|
|

楼主 |
发表于 3-2-2007 04:55 PM
|
显示全部楼层
how to do dis question?
if i want to use if and nested if only to answer dis question how is it?i have tried.bt duno how to continue.can anyone help me?
import java.util.*;
class year
{
public static void main(String [] args)
{
Scanner sc = new Scanner(System.in);
int year = sc.nextInt();
int d1,d2,d3,d4;
String roman = new String("I,V,X,L,C,D,M");
d1 = year%1000;
d2 = d1/100;
System.out.println(d2);
if ( d1>=900 || d1>=400 || d1>=100)
{
System.out.println(d1);
if (d2==9 || d2==4)
{
}
}
}
} |
|
|
|
|
|
|
|
发表于 3-2-2007 08:51 PM
|
显示全部楼层
Clock WorkIn Lab 1, we have seen how turtle graphics can be used to draw the clock face.
你用的是那一套turtle graphics?
程式碼麻煩貼一下。
另外,把for裡面的式子全部展開來就可以去掉array和string了...
還有,除的時候只需要除1000、100和10,至於500、50和5可以不用除,最後用if來判斷輸出文字。
[ 本帖最后由 MaokeJackson 于 3-2-2007 09:04 PM 编辑 ] |
|
|
|
|
|
|
|

楼主 |
发表于 3-2-2007 09:32 PM
|
显示全部楼层
im using bluej and turtle lor.
import java.util.*;
class time
{
public static void main(String [] args)
{
int hour, min, sec;
Scanner sc = new Scanner(System.in);
System.out.print("Enter hours: ");
hour = sc.nextInt();
System.out.print("Enter minutes: ");
min = sc.nextInt();
System.out.print("Enter seconds: ");
sec = sc.nextInt();
// compute the canonical time and display
sec = (hour*3600)+(min*60)+sec;
hour = (sec/3600)%60;
min = ((sec*60)/3600)%60;
sec = (sec%60);
System.out.print(hour);
System.out.print( ":" );
System.out.print(min);
System.out.print( ":" );
System.out.print(sec);
World world = new World();
Turtle s = new Turtle(world);
Turtle t = new Turtle(world);
Turtle u = new Turtle(world);
int hourAngle,minAngle, secAngle;
// compute the three angle associated with the
// second-, minute-, and hour- hand
secAngle = sec*6;
minAngle = (min*60+sec)/10;
hourAngle = (hour*3600+min*60+sec)/120;
System.out.println();
System.out.print("Angle of the sec hand: ");
System.out.println(secAngle);
System.out.print("Angle of the minute hand: ");
System.out.println(minAngle);
System.out.print("Angle of the hour hand: ");
System.out.println(hourAngle);
// draw the clock face using Turtle t
t.right(hourAngle);
t.forward(100);
s.right(minAngle);
s.forward(120);
u.right(secAngle);
u.forward(160);
}
}
This is the answer for the second task.however,i duno how to proceed to let the clock ticks.hope someone can help me.thanks. |
|
|
|
|
|
|
|
发表于 3-2-2007 10:38 PM
|
显示全部楼层
- import java.util.Calendar;
- ...
- public static void main(String[] args) throws InterruptedException {
- int hour, minute, second;
- Calendar now;
- while ( true ) {
- now = Calendar.getInstance();
- hour = now.get(Calendar.HOUR);
- minute = now.get(Calendar.MINUTE);
- second = now.get(Calendar.SECOND);
- System.out.println(hour + ":" + minute + ":" + second);
- Thread.sleep(1000);
- }
- }
复制代码 這樣就可以每秒跳一次了。
[ 本帖最后由 MaokeJackson 于 3-2-2007 10:49 PM 编辑 ] |
|
|
|
|
|
|
| |
本周最热论坛帖子
|