These exercises are intended to help you get familiar with Java. They do not include concurrency.
Getting familiar with your IDE
In your IDE, create a new project (or similar, if another name is used) called Lab3. As well as, the student has to create a Java class called Exercise1 with the main method. In the main method, print a text of your choice. Run the program, and make sure the output is produced as expected.
Variable, data types, operators
Create a new class in the project called Exercise2. In the main method, create the following variables and choose names as appropriate:
- Three int variables, initialized to 5, 3, and 0.
- Three float variables, initialized to 5.0, 3.0, and 0.0.
- A char variable, initialized to 'c'.
- A String variable, initialized to the text "This is a string".
- An array of three ints, initialized to 1, 2, and 3.
- A boolean variable initialized to false.
Then try various operations and print out their results using System.out.print(ln). Divide (all values mean the appropriate variable here) 5 by 3 and 5.0 by 3.0. Divide 5 by 3.0. Try appending an int and a float to the String and print the result. Try appending the char to the string. Try appending the appending 3 to the char. Calculate the sum of the three elements of the array (hard coding the operations is enough here). Check if 5+3 is an equal number and assign the result to the boolean variable.
Control structures
- Write a program that converts a hexadecimal number to a decimal number. You can ask the user for input or just assign the hexadecimal number to a String variable in the code. Print out the decimal number. Hint: A for loop should be the appropriate structure to use here.
- Add a check if the input is valid (i. e., consists of 0-9 and A-F). Check the individual letters for this. Print a message like "X is not allowed in a hexadecimal number" for each wrong character.
- Write a program that finds the smallest common divisor of two integer numbers. This can be done by subtracting the smaller number from the larger one until both numbers are the same. Print out the result.
Classes
- Write a class Person that models a person with attributes like name, birthday, and height. Add get and set methods for those attributes. Also, think about constructors. Write a method that returns the age of the person. Hint: An instance of Date is set to the current Date and time. Look at the documentation for Date to find out how you could calculate the age.
- Write a class Student which inherits from Person and adds attributes like student id, year, and course. Add a toString method that prints out a nice sentence about the student. Hint: You can use attributes from Person and Student here.
- Write a program that creates two variables of type Person and one of type Student. Assign a Person instance to one of the variables and a Student to the other. Which methods of Person and Student can you use? Now create a variable of type Student and assign a new instance of Student to it. Finally, create a second Student instance. Can you assign the Student assigned to a Person variable to it? Call some methods of those instances, including the toString method of Student.
- Consider the following code:
Person p1=new Person(); Person p2=p1(); Person p3=new Person(); System.out.println((p1==p2)+" "+(p1==p3)); p1=null;
- How many instances of Person are created during the execution of the program?
- What is the output of the println statement?
- Is the instance of Person, which was assigned to p1, still accessible after the last line of the program?
Composition and aggregation
Write a class Course which models a course. Put in some appropriate attributes (e. g. course title). Then add an option for any number of students to be registered on the Course. You need a proper data structure and methods to add and remove students. You could also have a method to remove all students and add several students simultaneously. Write a program creating a Course and several Students and make the Students join the Course.