KEMBAR78
Exercises Topic 1 FunProII | PDF | Class (Computer Programming) | Method (Computer Programming)
0% found this document useful (0 votes)
9 views10 pages

Exercises Topic 1 FunProII

1. The document presents a series of exercises on object-oriented programming in Java. The exercises include creating classes for employees, products, and vehicles, as well as implementing relationships between classes such as association and inheritance. It also includes exercises on abstract classes, polymorphism, and exception handling.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views10 pages

Exercises Topic 1 FunProII

1. The document presents a series of exercises on object-oriented programming in Java. The exercises include creating classes for employees, products, and vehicles, as well as implementing relationships between classes such as association and inheritance. It also includes exercises on abstract classes, polymorphism, and exception handling.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Fundamentals of Programming II

Theme 1. Classes and Objects

1. Create an employee class and test its functionality. The instance variables that
must consider their first name, last name, and annual salary. In addition to the methods of
consultation and updating of the variables, consider two more methods. One of them
it will return the monthly salary. The other method will return an extra calculated as a
percentage of the annual salary plus 1000. The percentage is a parameter that is passed to the
method.

2. Write a program that estimates the profit obtained from a particular product in
one month. The name of the product, the cost of the unit, the selling price is known.
public and the average number of units of the product sold in a month. Design and
use the appropriate classes.

3. Build a program that uses a class called vehicle, which has as


attributes the most characteristic properties of each vehicle. The program will create
a matrix of objects with different vehicles and will print the characteristics
from the cheapest vehicle. The data for each vehicle will be entered via keyboard.
when the program requests it.

4. The following class diagram, incomplete, represents the basic structure of a


Program to collect product orders from a company.

Order Client
1
orderID * name
date address
value
payment()

*
Product Client Client
corporate particular
ID
cost account cardNmr
payment()

Write a Java version of the Product class. Include methods that allow access to the
product information.

Exercises Topic 1 Page 1


Fundamentals of Programming II

5. Given the following association relationship between classes A and B. Indicate how it is
I would implement

1 1
A B

An object of A inherits from B


b) A method of B has an object A as a parameter
c) An instance attribute of B is an object A
d) None of the above is true

6. What is the result of the following program?

class Exercise {
public static void main(String[] args) {
Class1 obj1=new Class1();
obj1.print(24.3, 5);
}
}
class Class1 {
private double value=9.8;
private int x=7;
public void print(double value, int x) {
System.out.print(value + " " + this.x);
}
}

7. What does the following program print?

class Exercise {
public static void main (String [] args) {
Class1 obj1 = new Class1(5, 4);
System.out.print(obj1.modify(4) + " ");
Class1 obj2 = new Class1(5, 4);
System.out.print(obj2.modify(5)+" ");
obj2=obj1;
System.out.println(obj2.modify(5) + " ");
} //end main
}
class Class1{
int p1,p2;
public Clase1(int i, int j){
p1=i;
p2=j;
}
public int modify(int i){
p1=p1+i;
p2=p2+i;
System.out.print(p2 + " ");
return p1;
}
}

8. Develop a program that serves to evaluate the value of a polynomial, whose degree and
coefficients are entered via keyboard, at a given abscissa value.

Exercises Topic 1 Page 2


Fundamentals of Programming II

9. What is the error in the following program?

class Account {

protected int account_number;


protected double balance;

public Cuenta(int account, double initial) {


account
balance = initial;
} // End of account constructor

public void deposit(double amount) {


balance = balance + amount;
} // End deposit method

public double balance() {


return balance;
} // End balance method
}//end class Account

class Bank {

public static void main(String[] args) {


The account is created
Account account_1 = new Account(123456, 2505.32);
The balance is queried
double total_bill=account_1.balance;
Total actually in the account:
+total_account + ' Euros'
translated
}//from the Bank class

10. a) Design and implement in Java a class to work with isosceles triangles. For this,
define the minimum instance variables it requires, provide query methods, a
constructor method and implement other methods to calculate the perimeter and the area of a
triangle. b) Implement a method in Java that returns from a matrix of triangles
the area of the triangle with the largest surface

11. A theater is characterized by its name and its address, and it hosts 4 performances a day.
Each function has a name and a price. Design the classes and indicate which methods
it would have each class, taking into account that the name of the theater and the name can be changed
and price of the function. Implement these classes.

Exercises Topic 1 Page 3


Fundamentals of Programming II

12. a) What does the following program print?

class Exercise {
public static void main (String [] args) {
Class1 obj1 = new Class1(7);
Class1 obj2 = new Class1(9);
obj2=obj1;
System.out.print(obj1.valorprop() + " ");
System.out.print(obj2.propertyValue() + " ");
}//end main
}

class Class1{
static int prop=0;
public Clase1(int value) {
value
}
public int valorprop(){
return prop;
}
}

b) What if we don't declare prop in Class1 as static?

13. What is the error in the following class?


class Example {

static private final int a=10;


static private final int b=20;
public int getA() {return a;}
public int getB() {return b;}

}
a) No errors occur, the class is well implemented
b) a and b are static so they cannot be declared in a class
c) Since a and b are static, they cannot be used in the methods getA and getB, which are not
static
d) a and b cannot be static and final at the same time

14. Given the following code snippet:

Ordenador sobremesa
desktop = new Computer();
dessert.price(900);
laptop = new Computer();
portatil.precio(1100);
desktop
sobremesa = null;

Which statement is true?

In the end, both the desktop object and the portable object point to null.
At the end, there is only one object of type Computer priced at 1100.
c) In the end, there is only one object of type Computer with a price of 900.
d) In the end, there are two objects of type Computer, one with a price of 900 and another with a price of 1100

Exercises Topic 1 Page 4


Fundamentals of Programming II

15. Given the following code snippet that belongs to a perfectly functioning program
defined

Student a = new Student("Elena");


Student b = a;
Student c = new Student("María");
a = new Student("Julia");
b = c;
c = null;

Which students are accessible through the program references at the end of the fragment?
of code?:

a) Julia
b) María and Julia
c) Elena, Julia and María
d) None of the above answers is correct

16. Let a and b be two real numbers*, the pair (a, b) is called a complex number if the equality, the
Addition and multiplication of pairs are defined as follows:

a) Equality: a  c,d  a c  b d
 c,d   a c,b d
b) Sum:a,b 
 ac  bd,ad
c) Product:a,b c,d  bc 

The complex number (a, b) can be expressed in the form a+bi. It can also be defined as
multiplication operation of a complex number by an integer in the following way:
Multiplication by an integer: (a,b) ( a, b)
With this definition, create a complex number class.

To achieve this:
a) Draw the UML diagram of that class indicating all its members.
b) Implement the class code in Java.

Note: For simplicity, consider a and b as integers.

17. Build a program that displays the result of the sum and the product on the screen
two complex numbers using the complex class implemented in the previous exercise.
In addition, the program must check if two given complex numbers are equal and
It should also multiply a complex number by an integer. The program must display for
display the complex number (in the form x+yi) resulting from the sum and the product of two
given complex numbers, as well as the result of the multiplication of a complex number
for an integer. In addition, the program should print a message indicating whether the two
complex numbers are equal or not.

Exercises Topic 1 Page 5


Fundamentals of Programming II

18. What does the following program print?

class Principal {

public static void main(String[] args) {


Author author = new Author("Paul", "Auster");
Book book = new Book("The New York Trilogy", author);
System.out.println(libro.getTitle() + " by " +
(book.getAuthor()).toString());
}
}

class Book {
private String title;
private Author author;

public Book(String title, Author author) {


this.title = title;
this.author = author;
}

public Author getAuthor() {


return author;
}

public String getTitle() {


return title;
}
}

class Author {

private String name;


private String lastName;

public Author (String name, String surname) {


this.name = name;
lastName
}

public String toString() {


return firstName + " " + lastName;
}
}

19. There is the following dependency relationship between two classes:

T
lol Truck

Write the code of both classes knowing that the truck class is characterized by the number
of the truck's axles and the number of tons it carries. The toll class must calculate what
must pay for a truck (3 euros per axle and 10 euros per ton). Its sample variables are
number of trucks that have passed through the toll and money charged, which must be refunded
for your later consultation and printing.

Exercises Topic 1 Page 6


Fundamentals of Programming II

20. What does the following program print?

class Student {
Student 1
char group = 'A';

char returnGroup() {
return group;
}

void changeGroup(char newGroup) {


newgroup
}

void print() {
System.out.println("Name: " + name);
System.out.println("Group: " + returnGroup());
}
}

class Principal {
public static void main (String[] args) {
Student a1 = new Student();
System.out.println("Object a1:\n-----------\n");
a1.print();
Call to method prueba1(a1).
test1(a1);
a1.print();
System.out.println(" ");
Student a2 = new Student();
Object a2: -----------
a2.print();
Call to method prueba2(a2).
test2(a2);
In main: Outside of the prueba2 method.
a2.print();
}
static void test1(Student b) {
b.changeGroup('B');
}

static void test2(Student b) {


b = new Student();
b.changeGroup('B');
Inside the method prueba2.
b.print();
}
}

Exercises Topic 1 Page 7


Fundamentals of Programming II

21. In a store, each invoice is represented by its number and a series of products. Each
The product is characterized by its alphanumeric code and its price. It is requested:
a) Draw the class diagram indicating the relationships between them and the members
what is in each one. It must include methods to query the invoice number and the
products from the invoice and methods for updating the product data.
b) Implement those classes in Java
c) Implement a main program where an invoice is printed with the following
productos: (código: C1, precio: 24 euros), (código: H23, precio: 234 euros) y (código: M30,
price: 109 euros). Next, the second product on the invoice must be changed for
the following (code: K123, price: 247 euros) and reprint the invoice

An airplane is characterized by an identification number and its crew. The crew


is made up of 4 people, each characterized by their position and the number of days that
pounds per week. Requested:

a) Draw the class diagram indicating the relationships between them and the
members that are in each one. It must include methods for:
the inquiry about the aircraft identification number
ii. the consultation and updating of the members' data
crew.
b) Implement those classes in Java
c) Implement a main program where the aircraft data is printed including the
data of the 4 crew members. Use the following example:
a. ID_avion: 234
b. Crew member data:
i. pilot, days off: 4
ii. copiloto, días libres: 3
iii. sobrecargo, días libres: 2
steward, days off: 2

Next, the pilot's number of days off must be changed to 3 and printed again.
the updated crew data. Do not perform keyboard readings, initialize the data
within the method. Note: The minimum number of attributes has been considered to avoid
increase the complexity of the exercise

23. The aim is to computerize the information of a youth tennis championship. The information
what interests us about the championship is: couples who are going to compete (can compete a
number of couples), city where the competition will be held and street. Each couple
it must be formed by 2 people. For each couple, we must control the identifier of the
couple, which city do they represent and it must also be verified with a method if the couple is
valid or not. A couple is considered valid if the age of each person is between
18 to 25 years old, both inclusive. People are characterized by their name and age and
It should be possible to query both attributes. Therefore, there will be a person class with those attributes.
attributes and query methods. It is requested:

a) Draw in UML the class diagram indicating the relationships between them and the
members that are in each one. It must include methods for querying all the
attributes of the specimen, and all the methods necessary for the resolution of the
exercise.
b) Implement these classes in Java.
c) Implement a main program where the information of two couples is entered.
that they are going to compete, knowing that the championship will be held in Ciudad Real in the
Paseo de la Universidad street. As output information, all must be displayed.
información relativa al juego: donde se celebra, parejas inscritas (suponer dos), e
personal information of each member of the couple. Furthermore, it must be verified if the
The two involved couples are valid.

Exercises Topic 1 Page 8


Fundamentals of Programming II

24. Write a program that uses a linked list to manage a series of books.
identified by their title. The program must be able to keep adding books to the series
maintained and able to print the titles of the entire series when prompted. As an example
use los siguientes libros: “Niebla”, “La Metamorfosis” y “El callejón de los milagros”

25. What does the following program print?

class Colors {
public static void main(String [] args) {
List colors = new List();
colors.include("red");
colors.include("blue");
colors.include("green");
colors.print();
}
} // End main class

class Node {
private String color;
private Node next;
public Nodo(String color) {
this.color=color;
}
public void put(Node next_node) {
next=next_node;
}
public Node get() {
return next;
}
public String color() {
return color;
}
} // End of Node class

class List {
private Node first;
public Lista() {
first=null;
}
public void print() {
for (Node aux = first; aux.get() != null; aux = aux.get()) {
System.out.print(aux.color() + " ");
}
}
public void include(String string) {
Node element = new Node(string);
if (first == null) {
first
}
else {
Aux node;
for (aux = first; aux.get() != null; aux = aux.get());
aux.put(element);
}
}
} // End of List class

red blue green b) null c) red blue d) blue green

Exercises Topic 1 Page 9


Fundamentals of Programming II

26. What does the following program print?

class Awards {
public static void main(String [] args) {
Engineers premios_Turing = new Engineers();
turing_awards.insert("Frances Allen");
awards_Turing.insert("Edsger Dijkstra");
Turing_awards.insert("Dennis M. Ritchie");
turing_awards.print();
}
} // End main class

class Node {
private String engineer;
private Node next;
public Node(String eng) {
eng
}
public void put(Node next_node) {
next=next_node;
}
public Node recover() {
return next;
}
public String get_engineer() {
return engineer;
}
} // End of Node class

class Engineers {
private Node inicial;
public Engineers() {
initial=null;
}
public void print() {
for (Node a = initial; a != null; a = a.retrieve()) {
System.out.print(a.get_engineer() + " ");
}
}
public void insert(String string) {
Node unit = new Node(string);
if (initial==null) {
unit
}
else {
Node b;
for (b=inicial;b.retrieve()!= null;b=b.retrieve());
b.put(unit);
}
}
} // End of Engineers class

a) FrancesAllen Edsger Dijkstra Dennis M. Ritchie


b)FrancesAllenEdsgerDijkstra
FrancesAllen
d)nul

Exercises Topic 1 Page 10

You might also like