0% found this document useful (0 votes)
270 views3 pages

Java Programming Insights

The document provides responses to three questions. Q1 discusses programming languages that compile to other languages, providing ClojureScript which compiles to JavaScript as an example. Web containers like Tomcat can also convert JSP code to Servlet source code. Q2 explains why only one class can be public per file, noting that having multiple public classes could cause compilation errors if another class tries to access a class not in the same file. Q3 asks which statement is correct about Java, but the document does not provide the answer to this question.

Uploaded by

Shivam Shukla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
270 views3 pages

Java Programming Insights

The document provides responses to three questions. Q1 discusses programming languages that compile to other languages, providing ClojureScript which compiles to JavaScript as an example. Web containers like Tomcat can also convert JSP code to Servlet source code. Q2 explains why only one class can be public per file, noting that having multiple public classes could cause compilation errors if another class tries to access a class not in the same file. Q3 asks which statement is correct about Java, but the document does not provide the answer to this question.

Uploaded by

Shivam Shukla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Important Q&A Discussions!

Below are important Question & Answers taken from the Q&A discussions in this
section.

Q1. Can you please specify what are programming languages which are compiled to
another programming languages? (Related to lecture on Compilation)

ClojureScript (https://clojurescript.org) is one such language (a slight variant of


Clojure) and it comes with a compiler that compiles its programs into Java Script.
It looks like there are not many tools that do such translations. Such compilers
are also referred to as source-to-source compilers or transcompilers.

Also FYI, Clojure is also a language, whose programs once compiled run on JVM ~ the
platform on which Java programs run and is covered in detail in this course.

Web containers like Tomcat also convert JSPs code into Servlet source code before
compiling them into class file. Since we are learning about Java in this course,
you may not be aware of JSPs & Servlets, which fall under Java EE. But, just keep
in mind that there is source code to source code translation happening (from JSP to
Servlet). But, Web container like Tomcat is not a compiler. It is more of a Web
server, but can do some code translation + compiling translated code.

Q2. Why are we using public for only one class?

It is how it has been designed and I am not sure if the reason is documented. But
here is a good reason on why it is done that way. Let's assume we have two public
classes Y & Z in a file named Y.java. Now, let's assume a different class X is
using Z. Now, when we compile X, the compiler first tries to locate Z.class and
if it cannot find it, then it tries to locate Z.java so that it can compile it
automatically. But, since we only have Y.java, the class Z cannot be located and
hence we get a compilation error. So, we do need to place them in separate
files.

Task:ou will first type in the class as indicated in the question and then submit
it. Once submitted, you can click Next to view the Instructor Example, where the
solution will be shown for you to cross-check. I would also suggest to actually
code the class, compile and run on your end.

Questions for this assignment


Create a class called CurrencyConverter (CurrencyConverter.java) and it includes:

1. Following int variables representing currency exchange rates for different


countries relative to 1 US dollar. For instance the first variable is rupee and it
has a value 63 indicating that 1 US dollar is equal to 63 rupees. Similarly,
second variable is dirham and it has a value 3 indicating that 1 US dollar is
equal to 3 dirhams. Last 4 variables are a bit different and are included to
remind you about certain aspects of variable names, which were discussed in the
lectures: (a) _yen & $australian indicate that variables can start with underscore
and dollar in addition to letter (same applies to methods & class names too), b)
variable name dollar is not initialized with a value, i.e., it gets a default of
0. c) Final variable is Rupee and it is different from the first variable rupee as
both have different cases (keep in mind that Java is case-sensitive). In reality,
currency exchange rates are real numbers like 63.52 and will be represented by
other data types, but for now let's not worry about that.

rupee = 63;

dirham = 3; // UAE
real = 3; // brazilian

chilean_peso = 595;

mexican_peso = 18;

_yen = 107;

$australian = 2; // australian dollar

dollar;

Rupee = 63;

2. In the same class write a method called printCurrencies() whose return type is
void and it should print all the variables declared above in the class. Below are
two sample print statements and they would print 'rupee: 63' & '$australian: 2'.
So, '+' is a concatenation operator and it would append the value of the
variable to the string in double quotes. We know that '+' is used for addition and
that is the case when we are dealing with numeric values. However, when Strings are
involved, then it would work as concatenation operator appending one string to
another.

void printCurrencies() {

System.out.println("rupee: " + rupee);

System.out.println("$australian: " + $australian);

3. Include a main method and it should include following statements. As we know,


the first statement creates an object and in the second statement we are invoking
the printCurrencies() method on the created object.

CurrencyConverter cc = new CurrencyConverter();

cc.printCurrencies();

In addition to submitting your solution, I would also suggest to go ahead and


actually code the entire class (CurrencyConverter.java) in the basics directory and
compile and then run it and you should see all the currencies and their values
printed.

Q3 Which of the following statements is correct about the Java programming


language?

You might also like