Can you tell me why this wont compile? I am trying to run java an C together usi
ID: 3866578 • Letter: C
Question
Can you tell me why this wont compile?
I am trying to run java an C together using JNI.
==============================
convert.java
import java.util.Scanner;
public class convert
{
public native float convertFtoC (int farenheight);
public native int roundTemp (int farenheight);
public static void main(String[] args)
{
System.loadLibrary("Convert");
convert convert = new convert();
System.out.println("Welcome");
Scanner input = new Scanner(System.in);
int farenheight;
float celcius;
System.out.println("Please Enter a Farenheight");
while (!input.hasNextInt())
{
input.next();
System.out.println("Invalid Input. Please Try again");
}
farenheight = input.nextInt();
System.out.println("Farenheight entered is: " + farenheight);
farenheight = convert.roundTemp(farenheight);
System.out.println("Rounded Temp is : " + farenheight);
celcius = convert.convertFtoC(farenheight);
System.out.println("Celcius is: " + celcius);
}
}
=====================
convert.c:
#include "convert.h"
#include <stdio.h>
JNIEXPORT jfloat JNICALL Java_convert_convertFtoC
(JNIEnv *env,jobject obj, jint farenheight)
{
float celcius;
celcius = (farenheight - 32) *(5.00/9.00);
return celcius;
}
JNIEXPORT jint JNICALL Java_convert_roundTemp
(JNIEnv *env,jobject obj, jint int)
{
if (temp %5 == 4.00)
{
temp = temp + 1.00;
}
else if(temp %5 == 3.00)
{
temp = *temp + 2.00;
}
else if(temp %5 == 2.00)
{
temp = temp + 3.00;
}
return temp;
}
===========================
convert.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class convert */
#ifndef _Included_convert
#define _Included_convert
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: convert
* Method: convertFtoC
* Signature: (I)F
*/
JNIEXPORT jfloat JNICALL Java_convert_convertFtoC
(JNIEnv *, jobject, jint);
/*
* Class: convert
* Method: roundTemp
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_convert_roundTemp
(JNIEnv *, jobject, jint);
#ifdef __cplusplus
}
#endif
#endif
Explanation / Answer
import java.util.Scanner;
public class convert
{
public native float convertFtoC (int farenheight);
public native int roundTemp (int farenheight);
public static void main(String[] args)
{
System.loadLibrary("Convert");
convert convert = new convert();
System.out.println("Welcome");
Scanner input = new Scanner(System.in);
int farenheight;
float celcius;
System.out.println("Please Enter a Farenheight");
while (!input.hasNextInt())
{
input.next();
System.out.println("Invalid Input. Please Try again");
}
farenheight = input.nextInt();
System.out.println("Farenheight entered is: " + farenheight);
farenheight = convert.roundTemp(farenheight);
System.out.println("Rounded Temp is : " + farenheight);
celcius = convert.convertFtoC(farenheight);
System.out.println("Celcius is: " + celcius);
}
}
=====================
convert.c:
#include "convert.h"
#include <stdio.h>
JNIEXPORT jfloat JNICALL Java_convert_convertFtoC
(JNIEnv *env,jobject obj, jint farenheight)
{
float celcius;
celcius = (farenheight - 32) *(5.00/9.00);
return celcius;
}
JNIEXPORT jint JNICALL Java_convert_roundTemp
(JNIEnv *env,jobject obj, jint int)
{
if (temp %5 == 4.00)
{
temp = temp + 1.00;
}
else if(temp %5 == 3.00)
{
temp = *temp + 2.00;
}
else if(temp %5 == 2.00)
{
temp = temp + 3.00;
}
return temp;
}
#include <jni.h>
/* Header for class convert */
#ifndef _Included_convert
#define _Included_convert
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT jfloat JNICALL Java_convert_convertFtoC
(JNIEnv *, jobject, jint);
JNIEXPORT jint JNICALL Java_convert_roundTemp
(JNIEnv *, jobject, jint);
#ifdef __cplusplus
}
#endif
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.