Java Programming Assignment: Time Conversion and Roman Numerals
VerifiedAdded on  2023/06/04
|9
|904
|164
Homework Assignment
AI Summary
This Java assignment solution encompasses three distinct programming problems. The first part involves time conversion, allowing users to convert between 12-hour and 24-hour time formats. The second part focuses on Roman numeral conversion, where the program takes a Roman numeral as input and converts it into its decimal equivalent. The third part presents a river crossing problem, which simulates a scenario where individuals attempt to cross a river based on their step count and the distance to the river. The solution includes complete Java code for each problem, demonstrating the implementation of the required functionalities, including input handling, conditional statements, loops, and output formatting.

Java programming
= |
= |
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

Java programming
Question 1
package a2q1;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class A2Q1 {
public static void main(String[] args) {
Scanner n= new Scanner(System.in);
System.out.print("Enter the time format:");
int a=n.nextInt();
if(a==24){
System.out.print("Enter time:");
Scanner nu= new Scanner(System.in);
String inputi=nu.nextLine();
A2Q1 t= new A2Q1();
t.conv24(inputi);
}else
if(a==12){
Question 1
package a2q1;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class A2Q1 {
public static void main(String[] args) {
Scanner n= new Scanner(System.in);
System.out.print("Enter the time format:");
int a=n.nextInt();
if(a==24){
System.out.print("Enter time:");
Scanner nu= new Scanner(System.in);
String inputi=nu.nextLine();
A2Q1 t= new A2Q1();
t.conv24(inputi);
}else
if(a==12){

Java programming
System.out.print("Enter time:");
Scanner nu= new Scanner(System.in);
String inputi=nu.nextLine();
A2Q1 t= new A2Q1();
t.conv12(inputi);
}else{
System.out.print("Wrong entry");
}
}
public void conv12( String inputi){
DateFormat ft= new SimpleDateFormat("HH:mm");
DateFormat outputformat = new SimpleDateFormat("hh:mm aa");
Date da = null;
String output=null;
try{
da= ft.parse(inputi);
output = outputformat.format(da);
System.out.println(inputi+" to 12hrs clock system is" +output);
}catch(ParseException pe){
pe.printStackTrace();
}
System.out.print("Enter time:");
Scanner nu= new Scanner(System.in);
String inputi=nu.nextLine();
A2Q1 t= new A2Q1();
t.conv12(inputi);
}else{
System.out.print("Wrong entry");
}
}
public void conv12( String inputi){
DateFormat ft= new SimpleDateFormat("HH:mm");
DateFormat outputformat = new SimpleDateFormat("hh:mm aa");
Date da = null;
String output=null;
try{
da= ft.parse(inputi);
output = outputformat.format(da);
System.out.println(inputi+" to 12hrs clock system is" +output);
}catch(ParseException pe){
pe.printStackTrace();
}
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

Java programming
}
public void conv24(String inputi){
DateFormat ft= new SimpleDateFormat("hh:mm aa");
DateFormat outputformat = new SimpleDateFormat("HH:mm");
Date da = null;
String output=null;
try{
da= ft.parse(inputi);
output = outputformat.format(da);
System.out.println(output);
}catch(ParseException pe){
pe.printStackTrace();
}
}
}
}
public void conv24(String inputi){
DateFormat ft= new SimpleDateFormat("hh:mm aa");
DateFormat outputformat = new SimpleDateFormat("HH:mm");
Date da = null;
String output=null;
try{
da= ft.parse(inputi);
output = outputformat.format(da);
System.out.println(output);
}catch(ParseException pe){
pe.printStackTrace();
}
}
}
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

Java programming
Question 2
package a2q2;
import java.util.Scanner;
public class A2Q2 {
static String romanNumeral;
static int decimalNum;
public static void main(String[] args) {
A2Q2 rmtoD = new A2Q2();
rmtoD .convertToDecimal();
rmtoD .printRoman(romanNumeral);
}
public void convertToDecimal () {
Scanner scan = new Scanner(System.in);
System.out.print("Enter a Roman number: ");
romanNumeral = scan.nextLine();
romanNumeral = romanNumeral.toUpperCase();
int l= romanNumeral.length();
int num=0;
int previousnum = 0;
for (int i=l-1;i>=0;i--)
{
char x = romanNumeral.charAt(i);
x = Character.toUpperCase(x);
Question 2
package a2q2;
import java.util.Scanner;
public class A2Q2 {
static String romanNumeral;
static int decimalNum;
public static void main(String[] args) {
A2Q2 rmtoD = new A2Q2();
rmtoD .convertToDecimal();
rmtoD .printRoman(romanNumeral);
}
public void convertToDecimal () {
Scanner scan = new Scanner(System.in);
System.out.print("Enter a Roman number: ");
romanNumeral = scan.nextLine();
romanNumeral = romanNumeral.toUpperCase();
int l= romanNumeral.length();
int num=0;
int previousnum = 0;
for (int i=l-1;i>=0;i--)
{
char x = romanNumeral.charAt(i);
x = Character.toUpperCase(x);

Java programming
switch(x)
{
case 'I':
previousnum = num;
num = 1;
break;
case 'V':
previousnum = num;
num = 5;
break;
case 'X':
previousnum = num;
num = 10;
break;
case 'L':
previousnum = num;
num = 50;
break;
case 'C':
previousnum = num;
num = 100;
break;
case 'D':
previousnum = num;
num = 500;
break;
case 'M':
previousnum = num;
num = 1000;
switch(x)
{
case 'I':
previousnum = num;
num = 1;
break;
case 'V':
previousnum = num;
num = 5;
break;
case 'X':
previousnum = num;
num = 10;
break;
case 'L':
previousnum = num;
num = 50;
break;
case 'C':
previousnum = num;
num = 100;
break;
case 'D':
previousnum = num;
num = 500;
break;
case 'M':
previousnum = num;
num = 1000;
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

Java programming
break;
}
if (num<previousnum)
{decimalNum= decimalNum-num;}
else
decimalNum= decimalNum+num;
}
}
public static void printRoman (String romanNumeral){
System.out.println ("The equivalent of the Roman numeral "+romanNumeral+" is
"+decimalNum);
}
public static void printDecimal (int decimalNum){
System.out.println ("Decimal Number stored is: " + decimalNum);
}
}
break;
}
if (num<previousnum)
{decimalNum= decimalNum-num;}
else
decimalNum= decimalNum+num;
}
}
public static void printRoman (String romanNumeral){
System.out.println ("The equivalent of the Roman numeral "+romanNumeral+" is
"+decimalNum);
}
public static void printDecimal (int decimalNum){
System.out.println ("Decimal Number stored is: " + decimalNum);
}
}
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

Java programming
Question 3
package a2q3;
import java.util.Scanner;
public class A2Q3 {
public static void main(String[] args) {
int i;
System.out.println("Enter n:");
Scanner h = new Scanner(System.in);
int hi=h.nextInt();
System.out.println("Enter rock distsnce");
int []git=new int[hi];
for(i=0;i<hi;i++){
Scanner j = new Scanner(System.in);
git[i]=j.nextInt();
}
System.out.println("Enter m");
Scanner p = new Scanner(System.in);
int po=p.nextInt();
int []jj=new int[po];
String [] name=new String [po];
for(int y=0;y< po;y++){
Question 3
package a2q3;
import java.util.Scanner;
public class A2Q3 {
public static void main(String[] args) {
int i;
System.out.println("Enter n:");
Scanner h = new Scanner(System.in);
int hi=h.nextInt();
System.out.println("Enter rock distsnce");
int []git=new int[hi];
for(i=0;i<hi;i++){
Scanner j = new Scanner(System.in);
git[i]=j.nextInt();
}
System.out.println("Enter m");
Scanner p = new Scanner(System.in);
int po=p.nextInt();
int []jj=new int[po];
String [] name=new String [po];
for(int y=0;y< po;y++){

Java programming
Scanner j = new Scanner(System.in);
System.out.println("Enter name");
name[y]=j.nextLine();
System.out.println("Enter step");
jj[y]=p.nextInt();
}
int u;
for(u=0;u< hi;u++){
for(int t=0;t<po;t++){
if(jj[t]> git[u]){
System.out.print(name[t]+":"+jj[t] +"\n2");
}
}
}
System.out.print("The above crossed the river \n");
}
}
Scanner j = new Scanner(System.in);
System.out.println("Enter name");
name[y]=j.nextLine();
System.out.println("Enter step");
jj[y]=p.nextInt();
}
int u;
for(u=0;u< hi;u++){
for(int t=0;t<po;t++){
if(jj[t]> git[u]){
System.out.print(name[t]+":"+jj[t] +"\n2");
}
}
}
System.out.print("The above crossed the river \n");
}
}
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide
1 out of 9
Related Documents

Your All-in-One AI-Powered Toolkit for Academic Success.
 +13062052269
info@desklib.com
Available 24*7 on WhatsApp / Email
Unlock your academic potential
Copyright © 2020–2025 A2Z Services. All Rights Reserved. Developed and managed by ZUCOL.