Java — CSCI 134: Introduction to Computer Science CSCI 134: Introduction to Computer Science Calendar Syllabus Office and TA Hours Resources Programmer’s Toolbox Python Quick References Strings and Lists Viewing Feedback on Labs Python Style Guide Set Up Your Computer For CS 134 Set Up Your Mac Set Up Your Windows PC Set Up Your Windows PC (Non-Linux Version) How To Use Jupyter Notebooks Useful Links Think Python Textbook Computer Science Department Williams College Java Java¶ Although we can configure jupyter to run Java interactively, Java is not meant to be run interactive. Doing so in jupyter requires additional packages to be installed. Instead, we are going to use jupyter to create files and run commands as though we were running them on our Terminal. In this first example, we are creating a file called Hello.java. %%file Hello.java
public class Hello {
public static void main(String [] args) {
System.out.println("Hello, world!");
}
}
Overwriting Hello.java
Java programs must be compiled before we can run them. Compiling a java program creates a class file. Before compiling, let’s remove all previously compiled class files and look at what’s left. %%bash
rm *.class
ls Hello.*
Hello.java
Now let’s compile our program. After compiling, if we use ls again to see the contents of our directory, a class file now exists. %%bash
javac Hello.java
ls Hello.*
Hello.class
Hello.java
Now let’s run our program using the java command. %%bash
java Hello
Hello, world!
As an aside, note that we could use a similar technique with Python. It’s usually more convenient to run Python interactively, but it would work in the same way. %%file hello.py
def main():
print("Hello, World!")
if __name__ == "__main__":
main()
Overwriting hello.py
%%bash
python3 hello.py
Hello, World!
Note that this technique (of creating files and running commands as though we are on the Terminal) doesn’t work well when input is required, unfortunately. See the following example. def main ():
fahr = input ("Enter the temperature in F: " )
cel = (float(fahr) - 32) * 5.0/9.0
print ("The temperature in C is:" , cel)
if __name__ == "__main__":
main()
---------------------------------------------------------------------------
StdinNotImplementedError Traceback (most recent call last)
in
5
6 if __name__ == "__main__":
----> 7 main()
in main()
1 def main ():
----> 2 fahr = input ("Enter the temperature in F: " )
3 cel = (float(fahr) - 32) * 5.0/9.0
4 print ("The temperature in C is:" , cel)
5
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/ipykernel/kernelbase.py in raw_input(self, prompt)
843 """
844 if not self._allow_stdin:
--> 845 raise StdinNotImplementedError(
846 "raw_input was called, but this frontend does not support input requests."
847 )
StdinNotImplementedError: raw_input was called, but this frontend does not support input requests.
%%file TempConv.java
import java.util.Scanner;
public class TempConv {
public static void main (String args[]) {
Double fahr;
Double cel;
Scanner in;
in = new Scanner (System.in);
System.out.print("Enter the temperature in F: ");
fahr = in.nextDouble ();
cel = ( fahr - 32) * 5.0/9.0;
System.out.println("The temperature in C is: " +cel);
}
}
Overwriting TempConv.java
%%bash
javac TempConv.java
ls TempConv.*
TempConv.class TempConv.java
© Copyright 2022.