Java Programming Assignment: Time Conversion and Roman Numerals

Verified

Added 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.
tabler-icon-diamond-filled.svg

Contribute Materials

Your contribution can guide someone’s learning journey. Share your documents today.
Document Page
Java programming
= |
tabler-icon-diamond-filled.svg

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
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){
Document Page
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();
}
Document Page
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();
}
}
}
tabler-icon-diamond-filled.svg

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
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);
Document Page
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;
Document Page
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);
}
}
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
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++){
Document Page
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");
}
}
chevron_up_icon
1 out of 9
circle_padding
hide_on_mobile
zoom_out_icon
logo.png

Your All-in-One AI-Powered Toolkit for Academic Success.

Available 24*7 on WhatsApp / Email

[object Object]