Vous êtes sur la page 1sur 10

//1.

Develop a Java program, which defines a class named Person that contains as //attributes: name a protected String and x and y as protected integers. The cla ss will contain an explicit constructor without parameters that will initialize x and y with 0 and the string with null. A finalize() metho d will be used as a destructor from C++ and will display a message. Two mutator methods, setName() and setXY() will be defined to modify the attrib utes of the class. Instantiate 2 objects set the attributes with predefined values and display the content of the obtained objects

public class Declar { public static void main(String[] args) { Persoana ob1=new Persoana(); Persoana ob2=new Persoana(); ob2.setNume("ana"); ob2.setXY(3, 4); System.out.println(ob1.nume+" "+ob1.x+" "+ob1.y); System.out.println(ob2.nume+" "+ob2.x+" "+ob2.y); ob1=null; System.gc(); System.runFinalization(); } } class Persoana{ protected String nume; protected int x,y; Persoana(){ x=0; y=0; nume=null; } protected void finalize() { System.out.println("finalize ob"); x=0; y=0; nume=null; } public void setNume(String nume) { this.nume=nume; } public void setXY(int x, int y) { this.x=x; this.y=y; } }

//2. Consider a Circle class with 3 double public attributes, x, y, r. An explic it constructor //will initialize the values x, y, r, considering the same name for formal param eters and the this reference. Another constructor will modify the value of r by calling the previous construct or. As copy constructor a Circle object will be used as parameter and an empty const ructor will initialize with 0.0 the variables x and y and will store the value 1.0 in r. Instantiate at least 2 objects of Circle type usi ng the constructors from the class. Display the attributes of the objects.

class problema2 { public static void main(String args[]){ Circle ob1=new Circle(3,5,12); System.out.println("Constructor explicit"); System.out.println("x="+ob1.x); System.out.println("y="+ob1.y); System.out.println("r="+ob1.r); Circle ob2=new Circle(4.3); System.out.println("Constructor cu parametru:"); System.out.println("x="+ob2.x); System.out.println("y="+ob2.y); System.out.println("r="+ob2.r); Circle ob3=new Circle(); Circle ob4; ob4=ob3; System.out.println("Constructor copiere :"); System.out.println("x="+ob4.x); System.out.println("y="+ob4.y); System.out.println("r="+ob4.r); } } class Circle{ public double x,y,r; Circle(double x, double y, double r) { this.x=x; this.y=y; this.r=r; } Circle(double r) { this(2.3, 3.2, r); } Circle(){

x=0.0; y=0.0; r=1.0; } Circle(Circle cop){ this(cop.x,cop.y,cop.r); } }

//3. Consider a 2DCoordinates class with 2 protected integer attributes x and y. An explicit //constructor with 2 parameters will initialize the attributes and 2 accessor me thods getX() and getY() will return the values of x and y. A 3DCoordinates class, derived from the 1-st one, will consider a new protected integer z attribute. The explicit constructor with 3 parameters will call the 2D Coordinates constructor using the super mechanism. An accessor getZ() method wil l be defined. In the main class instantiate a 2DCoordinates object and two 3DCoo rdinates objects with predefined data. Using the accessor methods display the at tributes of the objects.

public class Problema3 { public static void main(String[] args) { a2D ob1=new a2D(3,4); a3D ob2=new a3D(2,3,4); a3D ob3=new a3D(5,3,5); ob1.getX(); ob1.getY(); ob2.getX(); ob2.getY(); ob2.getZ(); ob3.getX(); ob3.getY(); ob3.getZ(); } } class a2D{ protected int x; protected int y; a2D(int x, int y){ this.x=x; this.y=y; } void getX(){ System.out.println("Valoarea lui x: "+x); }

void getY(){ System.out.println("Valoarea lui y: "+y); } } class a3D extends a2D{ protected int z; a3D(int x, int y, int z){ super(x,y); this.z=z; } void getZ(){ System.out.println("Valoarea lui z: "+z); } }

//5. Define an abstract class named Coder that declares the prototypes of the fu nctions that //can code and decode a String or a integer variable according to a key received as parameter. //A second class named ExplicitCoder extends the first class and defines the inh erited abstract methods. A third class named ExplicitCoderExtended redefines the methods. Instantiate both non-abstract classes and make use of the existent met hods. Study the upcasting and the downcasting mechanisms.

protected class Coder { int x; public void Do () { System.out.println("Do something"); } void Do2 () { System.out.println("Do something new"); if(x==3) { Do(); } } protected class ExplicitCoder extends Coder { public void Do () { System.out.println("Do anything"); } } protected class ExplicitCoderExtended extends ExplicitCoder { public void Do() { System.out.println("Do anything 2"); } }

public class Nothing{ public void main (String args[]) { Coder firstCoder; ExplicitCoder cod1=new ExplicitCoder(); ExplicitCoderExtended cod2=new ExplicitCoderExtended(); cod1.Do(); cod1.Do2(); cod2.Do(); cod2.Do2(); cod1=cod2; firstCoder.Do(); firstCoder.Do2(); firstCoder=cod1; firstCoder.Do(); firstCoder.Do2(); ExplicitCoder firstExplicitCoder; firstExplicitCoder=new ExplicitCoder(); Coder secondCoder; secondCoder=firstExplicitCoder; secondCoder.Do(); ExplicitCoder secondExplicitCoder; secondExplicitCoder=(ExplicitCoder)secondCoder; secondExplicitCoder.Do(); } } }

//6. The first phones had just a few elementary functions: void sendSms (string message) //? write text message //? receive text message void getSms (string message) //? view text message void viewSms() //? receive a call void getCall (string IDCaller, string callMessage) //? make a call void doCall (string IDCaller, string callMessage) //? view call message void viewCall() //and following variables: //? SmsMemory //? IdMobile //? callMessage //Write an interface, mobilephone, which defines these functions and variables a nd then define the class Nokia which implements this interface. The class has a constructor with no parameters which sets all variables on and a constructor whi ch receives as parameter the mobile phone's number and then calls the constructo r with no parameters. //Write a new interface, smartphone, which extends the previous interface and de fines the following variable: //? operating system //and the corresponding getter and setter functions for it. //Define 2 new classes Samsung and Apple, which implement the new interface and have also a constructor with no parameters and one which sets the own phone numb er and afterward calls the constructor with no parameters. //Define an object of mobilephone type initialized with a Nokia object and then define 2 objects of smartphone type initialized with Samsung and Apple. Implemen t following scenario: //? Apple sends a message (read from the Keyboard) //? Nokia gets the message the content of the message is displayed via viewSMS

//? The operating system is set for Samsung (Android 4.0.4) //? Samsung calls Apple (and sends a message from the Keyboard) //? Apple gets the call displays the content via viewCall

package lab4_pb6; import java.util.*; import java.util.Scanner; interface mobilephone { public String memorieSMS="256 caractere"; public String IdMobil="super231g"; public String mesajApel="Dling dlang dlong"; public void sendSms(String message); public void getSms(String message); public String viewSms(); public void getCall(String IdApelant, String mesajApel); public void doCall(String IdApelant, String mesajApel); public String viewCall(); } class Nokia implements mobilephone { Nokia() { final String memorieSMS=""; final String IdMobil=""; final String mesajApel=""; } Nokia(int nr_tel) { super(); int nr_telefon=nr_tel; } public void sendSms(String message) { String s; s=message; System.out.println("Nokia a trimis mesajul:"+s); } public void getSms(String message) { String s; s=message; System.out.println("Nokia a primit mesajul:"+s); } public String viewSms() { return("Press Enter on Nokia to view your SMS"); } public void getCall(String IdApelant, String mesajApel) { System.out.println(IdApelant+" is calling you on Nokia"); System.out.println("Mesajul primit de Nokia este:"+mesajApel+" o n Nokia");

} public void doCall(String IdApelant, String mesajApel) { System.out.println("I wanna call from Nokia "+IdApelant); System.out.println(mesajApel+" from Nokia"); } public String viewCall() { return("Press Enter on Nokia to view your missed Call"); } } interface smartphone extends mobilephone { public String sistem_de_operare="Android 4.0.4"; String setter(String sistem); void getter(String sistem); } class Samsung implements smartphone { Samsung() { final String memorieSMS=" "; final String IdMobil=" "; final String mesajApel=" "; final String sistem_de_operare=" "; } Samsung(int nr_tel) { super(); int nr_telefon=nr_tel; } public String setter(String sistem) { String sistem_de_operare=sistem; return sistem_de_operare; } public void getter(String sistem) { String sistem_de_operare=sistem;; } public void sendSms(String message) { String s; s=message; System.out.println(" Samsung a trimis mesajul"+s); } public void getSms(String message) { String s; s=message; System.out.println(" Samsung a primit mesajul"+s); } public String viewSms() { return("Press Enter on Samsung to view your SMS"); } public void getCall(String IdApelant, String mesajApel)

{ System.out.println(IdApelant+" is calling you on Samsung"); System.out.println("Mesajul primit de Samsung este:"+mesajApel); } public void doCall(String IdApelant, String mesajApel) { System.out.println("I wanna call from Samsung "+IdApelant); System.out.println(mesajApel); } public String viewCall() { return("Press Enter on Samsung to view your missed Call"); } } class Apple implements smartphone { Apple() { final String memorieSMS=" "; final String IdMobil=" "; final String mesajApel=" "; final String sistem_de_operare=" "; } Apple (int nr_tel) { super(); int nr_telefon=nr_tel; } public String setter(String sistem) { String sistem_de_operare=sistem; return sistem_de_operare; } public void getter(String sistem) { String sistem_de_operare=sistem;; } public void sendSms(String message) { String s; s=message; System.out.println("Apple a trimis mesajul:"+s); } public void getSms(String message) { String s; s=message; System.out.println("Apple a primit mesajul "+s); } public String viewSms() { return("Press Enter on Apple to view your SMS"); } public void getCall(String IdApelant, String mesajApel) { System.out.println(IdApelant+" is calling you on Apple"); System.out.println("Mesajul primit de Apple este:"+mesajApel);

} public void doCall(String IdApelant, String mesajApel) { System.out.println("I wanna call from Apple"+IdApelant); System.out.println(mesajApel); } public String viewCall() { return("Press Enter on Apple to view your missed Call"); } } public class testpb6 { public static void main(String args[]) { Nokia obnok=new Nokia(); Samsung obsam=new Samsung(); Apple obapp=new Apple(); Scanner scan = new Scanner (System.in); System.out.println("Dati mesajul pe care il va trimite Apple"); String mesajapp_nok=scan.nextLine(); obapp.sendSms(mesajapp_nok); System.out.println(obnok.viewSms()); obnok.getSms(mesajapp_nok); obsam.setter("Android 4.0.4"); Scanner scan2 = new Scanner (System.in); System.out.println("Dati mesajul pe care il va trimite Samsung") ; String mesajsam_app=scan2.nextLine(); obsam.doCall("Apple200",mesajsam_app); obapp.viewCall(); obapp.getCall("Samsung5",mesajsam_app); } }

//7. Write a class whose instances represent a full deck of cards. The playing c ards have //only 2 properties: rank and color. Verify if a set of 3 randomly extracted car ds can result in 3 consecutive ranks, 3 cards of the same color or 3 cards of th e same rank. The extracted cards are not placed back in the deck. Display the fo und combinations or -N- if no solution was found.

import java.util.*; public class deck { public static String[] rank={"2","3","4","5","6","7","8","9","10","J" ,"Q","K","A"}; public static String[] color={"rosu","negru","romb","trefla"}; public static Integer check[][]=new Integer[4][13]; public static String[][] cards=new String[4][13]; public static void deckgen(){ for(int i=0;i<4;i++)

for(int j=0;j<13;j++) { check[i][j]=1; cards[i][j]=rank[j]+" "+color[i]; } } public static void main(String args[]) { int c=0; int[] v1={0,0,0}; int[] v2={0,0,0}; deckgen(); for(int k=0; k<17;k++) { for(int i=0;i<3;i++) { do{ v1[i]=(int)(4*Math.random()); v2[i]=(int)(13*Math.random()); }while(check[v1[i]][v2[i]]==0); check[v1[i]][v2[i]]=0; } if(v1[0]==v1[1]&&v1[1]==v1[2]) { c=1; for(int j=0;j<3;j++) System.out.println(cards[v1[j]][v2[j]]+" "); } if(v2[0]==v2[1]&&v2[1]==v2[2]) { c=1; for(int j=0;j<3;j++) System.out.println(cards[v1[j]][v2[j]]+" "); } Arrays.sort(v2); if((v2[1]==v2[0]+1&&v2[1]==v2[2]-1)) { c=1; for(int j=0;j<3;j++) System.out.println(cards[v1[j]][v2[j]]+" "); } } if(c==0) System.out.println("-N-"); } }

Vous aimerez peut-être aussi