LAPORAN ECLIPS
PENGENALAN PEMROGRAMN
Dosen Pengampu : Safuan S.Kom, M.Kom
Disusun oleh :
DENI NUR ZAMAN_C2C023037
PRODI S1 INFORMATIKA
UNIVERSITAS MUHAMMADIYAH SEMARANG 2023
A.Pendahuluan
Pustaka Eclipse adalah sebuah IDE (Integrated Development Environment) untuk mengembangkan perangkat lunak dan dapat dijalankan di semua platform (platform-independent). Dalam pengabdian ini digunakan android sebagai platformnya.
B.Tujuan
Untuk lebih mendalami sesuatu tentang pemrograman java terutama pada kelas,object ,metode,parameter dan overloading
C.Daftar Pustaka
https://publikasi.mercubuana.ac.id/index.php/jam/article/download/60 59/2671#:~:text=Eclipse%20adalah%20sebuah%20IDE
%20(Integrated,ini%20digunakan%20android%20sebagai
%20platformnya.
D.Pembahasan
7-1
Classes, Objects, and Methods Practice Activities
1. Create a simple class Shape that will represent a 2-dimensional shape with line segments for edges. It should have the following instance variables:
numSides (int), regular (boolean). Create at least two constructors and getter and setter methods.
2. Identify the key parts of the Java Class below. Put asterisks next to all the instance variables. Place a box around each constructor. Circle the signature of methods other than the constructor method. Place triangles around the
parameters.Underline the return types of methods.
public class Animal { int weight, height;
double speed;
Animal() { weight = 50;
height = 4;
speed = 2; //miles per hour }
Animal(int w, int h, int s ) { weight = w;
h = height;
speed = s }
public double getTime(double miles) { //gets the number of hours to go these miles
return miles/speed;
}
public int getWeight() { return weight;
}
public int getHeight() { return height;
}
public double getSpeed() { return speed;
} }
tanda bintang (*) di sebelah variabel instan, kotak di sekitar setiap konstruktor, tanda tangan metode selain konstruktor diidentifikasi dengan tanda △, dan garis bawahi jenis metode pengembalian diidentifikasi dengan tanda ▁.
3. Write code to create two instances of the Animal class template listed in problem #2. Be sure to use each of the two constructors provided. Then add Java code that will print the following:
a. Animal #1 has a speed of ___.
b. Animal #2 has a speed of ___.
Be sure that the blanks are automatically filled in with the actual speeds. Use the methods provided to access the speeds.
4. Write a class Student. It should have the following instance variables for the name, credits, grade point average (GPA), and quality Points. Create a
constructor method. Create two other methods as follows:
a. A method that will return the current grade point average which will be the quality points divided by the credits.
b. A method that will take in the credits for a class or semester along with the quality points. It should update the credits, the quality points, and the GPA.
5. Using the class you created in #4, create three instances of the Student Class from the table below:
Name Credits Quality Points Mary Jones 14 46
John Stiner 60 173 Ari Samala 31 69
6. Using the instance variables created in #5, add 13 credits and 52 quality points to the student “Ari Samala”.
7. Using the Card class from the slides and test the program to make sure it works. Add a second random Card. Code is included below:
public class Card{
String suit,name;
int points;
Card(int n1, int n2){
suit = getSuit(n1);
name = getName(n2);
points = getPoints(name);
}
public String toString(){
return "The " + name + " of " + suit;
}
public String getName(int i){
if(i == 1) return "Ace";
if(i == 2) return "Two";
if(i == 3) return "Three";
if(i == 4) return "Four";
if(i == 5) return "Five";
if(i == 6) return "Six";
if(i == 7) return "Seven";
if(i == 8) return "Eight";
if(i == 9) return "Nine";
if(i == 10) return "Ten";
if(i == 11) return "Jack";
if(i == 12) return "Queen";
if(i == 13) return "King";
return "error";
}
public int getPoints(String n){
if(n == "Jack" ||n == "Queen" ||n == "King"||n == "Ten") return 10;
if(n == "Two") return 2;
if(n == "Three") return 3;
if(n == "Four") return 4;
if(n == "Five") return 5;
if(n == "Six") return 6;
if(n == "Seven") return 7;
if(n == "Eight") return 8;
if(n == "Nine") return 9;
if(n == "Ace") return 1;
return -1;
}
public String getSuit(int i){
if(i == 1) return "Diamonds";
if(i == 2) return "Clubs";
if(i == 3) return "Spades";
if(i == 4) return "Hearts";
return "error";
} }
public class Main {
public static void main(String args[]){
int suitNumber = (int)(Math.random()*4.0+1);
int faceNumber = (int)(Math.random()*13.0+1);
Card newCard = new Card(suitNumber,faceNumber);
System.out.println(newCard);
} }
8. Add code to the Main class in exercise #7 to the following:
a. Display the total point value for the two random cards.
b. Ask the user if they would like another card. If they say yes display the new card and the points for all 3 cards in their“Hand”.
c. Loop to allow the user to continue to add cards to the hand until the number of points goes over 21 or the user decides not to add any more cards or the total number of cards is 5.
7-2
Parameters and Overloading Methods Practice Activities
1. Create a segment of code that initializes a public class Fish. Let the class contain a String typeOfFish, and an integer friendliness. Do not set values to these variables yet. These are instance variables and will be set inside the class constructors.
2. Create a public constructor (a method with the same name as the class) inside the class Fish. This constructor should take in no arguments. Inside the
constructor, set typeOfFish to “Unknown” and friendliness to 3, which we are assuming is the generic friendliness of fish.
3. Create another public constructor inside the class Fish. Have this constructor take in a string t and an integer f. Let typeOfFish equal t, and friendliness equal f.
4. Explain why is is possible to have more than one constructor with the same name and different arguments.
dimungkinkan untuk memiliki lebih dari satu konstruktor dengan nama yang sama tetapi dengan argumen yang berbeda. Ini dikenal sebagai overloading konstruktor. Overloading konstruktor memungkinkan kelas untuk memiliki beberapa cara untuk menginisialisasi objek, tergantung pada kebutuhan pengguna.
5. Create a method inside the class Fish called getFriendliness(), which takes in no arguments and returns the friendliness level of the fish.
6. Write a segment of code that initializes 2 new fish as defined below:
a. Fish 1: Name – Amber, Type – AngelFish, Friendliness level – 5 (very friendly) b. Fish 2: Name – James, Type – Guppy, Friendliness level – 3 (neutral)
7. Create a method nicestFish that takes in two fish as parameters, compares the friendliness level of two fish, and returns the fish with the higher
friendliness. Test this method with the fish defined in problem 6. (Friendliness scale: 1 mean, 2 not friendly, 3 neutral, 4 friendly, 5 very friendly) Hint:
fishName.getFriendliness() gives you the integer number of the friendliness of fishName. You have already created getFriendliness() in problem 5.
8. Modify the method nicestFish() to take be a variable argument method that takes in a variable number of fish and returns the nicest fish out of the fish it is
given. Hint: Inside of the method, create a new fish called temp. Set temp equal to the first fish passed into the method. Use a for loop to go through all the fish passed into the method and if you discover a fish that is more friendly than temp, set temp equal to that fish. After the for loop is complete, temp should be the friendliest fish. Return temp.
9. Test your method nicestFish() with the fish described in problem 6. Which fish is returned?
Ikan Bernama james berjenis guppy yang dikembalikan
10. Determine the best access modifier for each of the following situations:
a. A class Employee records the name, address, salary, and phone number.
Name: private (hanya dapat diakses di dalam kelas) Adress: private (hanya dapat diakses di dalam kelas) Salary: private (hanya dapat diakses di dalam kelas)
Phone number: private (hanya dapat diakses di dalam kelas)
b. An adding method inside of a class BasicMath that is also used in the Algebra class