Object-oriented Programming and Java Statement

Topics: Java, Object-oriented programming, Subroutine Pages: 5 (869 words) Published: January 14, 2014
1. What is the Java source filename extension? What is the Java bytecode filename extension?

Answer: .Java(Source code)
.Class(Compile Code)

2. Write a Java statement to display the string “The value is 100” to a user in a plain dialog box?

Answer: System.out.print (“ The value is 100”);

3. What is the command you would use to compile the file Addition.java?

Answer:javac Addition.java

4. What is the command you would use to execute the Addition class? Answer: java Addition

5. What does a Java class contain that identifies it as a Java application?

6. Write a Java statement that declares a constant called MAX which has a value of 1000.

Answer: int pie = 3.14;
Final int = 1000;

7. How do C++ chars differ from Java chars?

Answer: C and C++ strings are null-terminated array of characters; there is no real entity in C and C++ that is a string. Java strings are first-class objects.

Java language does not support three data types that are part of C and C++ Struct , union and pointer. C++ and C allow you to provide a variable number of arguments to a function. Java doesnot support that.

8. Given a double variable called dval, write a single Java statement to prompt the user to enter the price of an item using JOptionPanes, convert the result and store it into dval.

Answer: double dval= Double.parsDouble(JOptionPane.showInputDialog(“Please enter Price”));

9. Write one Java statement to display the string “The average is xxx” to the console, where the xxx displays the value from the double variable sum divided by 3. Make sure the value is displayed with 3 digits following the decimal point Answer: double sum = 100/3;

System .print.out (“ the average is” + sum); Decimal format d format = new decimal format;
System.print.out.ln( “the average is + dformat.format(sum));

10.Write one Java statement to create a string containing “The average is xxx” where the xxx contains the formatted value from the double variable sum divided by 3. Make sure the formatted string contains only 2 digits following the decimal point. Store the formatted string into a String variable called str. Answer:

11.Explain the difference between an object reference in Java and an object. Use an example to illustrate your answer.

Answer: For Example we have a class called Car;
Andrew Car CR= Andrew Car ( );

When u create an instance of a class you call class construction which then creates an instance of a class which u make equal to any variable.

Andrew Car A = new Andrew Car( );
A is just a pointer where it is pointing towards Andrew Car. We can make as many references as we want. But it will just point to the class.

12. Explain how objects are created in Java and how the memory used by objects is recovered. Compare this to object creation and destruction in C++.

Answer: To create an object, we first have to Create a class. Let’s create a class called car.

Public class car{

Now we need the characteristics of the car. Car has wheels, length & weight.

We need variables to store this trait.

Public class car{
private int wheels;
private Boolean length;
private string weight;

Since all objects have default constructor
Public Car( ){

Now create a new Java file called car creator.
Public class Car creator{
Public static void main(string [ ] arg) {
Car Car 1= new car;

Car1.set wheels (4);
Car1. set height(“tall”);
Car1. Set weight(“heavy”);
Systems.out. print ln(“ this car has “ + car1 get wheels()+”wheels”); Systems.out. println(“this car has” +car1.set height()); Systems.out.print ln(“this car has” +car1.set weight());

13.Explain how primitive values like doubles are passed between functions in Java. How are string objects passed between functions in Java?

Answer: They are passed by Value....
Continue Reading

Please join StudyMode to read the full document

You May Also Find These Documents Helpful

  • Object Oriented Programming Essay
  • Object Oriented Programming Essay
  • Lab Exercises: Object Oriented Programming with Java Essay
  • Reusability: Object Oriented Programming Essay
  • Object Oriented Programming Midterm Essay
  • Object-oriented Programming and Type Essay
  • Lecture: Object-oriented Programming and A. Variable B. Essay
  • 03: Object-oriented Programming and Cups Essay

Become a StudyMode Member

Sign Up - It's Free