Q1 What is wrong with the following variable declarations 1
Q1. What is wrong with the following variable declarations? (1 Mark).
int Km*h = 90; String double = student1; int int1 = \"50\"; String newCourse = \"CS-140\" int y = x; String 1_Car = \"BMW\";
Q2. What does this sequence of statements print? (1 Mark).
String msg = \"The number of characters in the title of this course is: \"; String newCourse = \"Computer Programming I\"; int numberOfCharacters = newCourse.length(); String courseUpperCase = newCourse.toUpperCase(); System.out.print(newCourse); System.out.println(courseUpperCase); System.out.print(msg); System.out.println(newCourse.length()); System.out.print(\"Welcome to : \"); System.out.println(newCourse); newCourse.replace(\"I\", \"II\"); System.out.print(\"Good luck \"); //System.out.println(“to all students”); System.out.print(\"for the course \"); System.out.println(newCourse);
Ministry of Higher Education Saudi Electronic University College of Computing and Informatics
Q3. Given the following Java code segment, answer the three questions below:
public class BankAccount { private double balance; public BankAccount() { balance = 0; }
public BankAccount(double initialBalance) { balance = initialBalance; System.out.print(\"Balance is: \"); System.out.println(balance + 10); } }
1- Is “public BankAccount()” considered as a constructor? Why? 2- What is the output of \"new BankAccount(10.5) \"; 3- Write a method that may be used to withdraw an amount? (1 Mark).
Q4. Implement a class Employee. An employee has a name (string) and a salary (double).
1- Write a default constructor. 2- Write a constructor with two parameters (name and salary). 3- Write methods to return the name and salary. (1 Mark).
Q5. In the 2- dimensional plane, a point is described by its two coordinates x and y. It supports these operations: - A constructor allowing initialization of coordinates - 4 methods to set and get the values of the coordinates x and y - Translation of a point
1- Write a class, called MyPoint that corresponds to an abstraction of points in the dimensional plane. 2- Provide a tester class that creates one point, then translate and display the new coordinates
Solution
Q1.int Km*h=90; - here \'*\' cannot be used
String double= student1; - String has to be defined under double quotes \" \"
int int1=\"50\";- here an integer must not be declared in double quotes
String newCourse = \"CS-140\" - The semicolan is missing here
int y = x; - here x may not be declared
String 1_Car = \"BMW\"; - variable name cannot start with a digit
Q2. The sequence of statements are:
1.Computer Programming I
2.COMPUTER PROGRAMING I
3.The number of characters in the title of this course is:20
4.Welcome to :Computer Programming I
5.Good luck for the course Computer Programming II
Q3. 1.Yes it is a constructor. A constructor has the same name as the class and does not have a data type. The above mentioned is a default constructor which has no parameters.
2. The output for the following question is : Balance is: 20.5
Q4.class Employee{
String name;
double salary;
Employee(){ name = \"null\"; salary=0.0; }
Employee(String n, double s)
{
name=n;
salary=s;
}
return_name()
{return name;}
return_salary()
{return salary;}
}

