0% found this document useful (0 votes)
74 views32 pages

String: Class Private Public

The document defines several Java classes including Student, Page, Book, Library, Animal, Dog, Snake, and Test classes. It demonstrates inheritance, polymorphism, interfaces, inner classes, generics, and collections. Singleton and pizza classes with subclasses are also defined to showcase inheritance and wildcards.

Uploaded by

Alexandru Vuței
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views32 pages

String: Class Private Public

The document defines several Java classes including Student, Page, Book, Library, Animal, Dog, Snake, and Test classes. It demonstrates inheritance, polymorphism, interfaces, inner classes, generics, and collections. Singleton and pizza classes with subclasses are also defined to showcase inheritance and wildcards.

Uploaded by

Alexandru Vuței
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 32

class Student {

private String name;


public int averageGrade;

// (1) constructor fr parametri
public Student() {
name = "Necunoscut";
averageGrade = 5;
}

// (2) constructor cu 2 parametri, folosit dac cunoatem numele i media
public Student(String n, int avg) {
name = n;
averageGrade = avg;
}

// (3) constructor cu un singur parametru, folosit atunci cnd cunoatem doar numele
studentului
public Student(String n) {
this(n, 5); // Astfel, se va apela constructorul (2)
}

// (4) metoda set pentru cmpul name al clasei Student
public void setName(String n) {
name = n;
averageGrade = 5;
}

// () metoda getter pentru cmpul name
public String getName() {
return name;
}
}
class Page {
private String content;
public int no;

public Page(String c, int no) {
tis!content = c;
tis!no = no;
}
}

class "oo# {
private String title; // compunere
private Page$% pages; // compunere
private &ibrar'(o) librar'(o) = null; // agregare

public "oo#(int dim, String title, &ibrar'(o) librar'(o)) {
tis!librar'(o) = librar'(o);
tis!title = title;
pages = ne) Page$dim%;
*or (int i=+; i , dim; i--)
pages$i% = ne) Page("Pagina " - i, i);
}
}

class &ibrar'(o) {
private String ro)Name = null; // agregare
public &ibrar'(o)(String ro)Name){
tis!ro)Name = ro)Name;
}
}

class &ibrar' {

// mem!ers" arra# of li!rar# ro$s
public void add(o)(&ibrar'(o) ro)) {!!!}
public static void main(String args$%){
&ibrar'(o) ro) = ne) &ibrar'(o)("a.");
"oo# boo# = ne) "oo#(.++, "title", ro));
boo# = null;
}}
class /nimal {
public void eat() {
S'stem!out!println("/nimal eating");
}
}

class 0ol* extends /nimal {
public void o)l() {
S'stem!out!println("0ol* o)ling");
}

public void eat() {
S'stem!out!println("0ol* eating");
}
}

class Sna#e extends /nimal {
public void bite() {
S'stem!out!println("Sna#e biting");
}
}

class 1est {
public static void main(String$% args) {
/nimal a $% = new /nimal$2%;

a$+% = new 0ol*();
a$.% = new Sna#e();

*or (int i = +; i , a!lengt; i--) {
a$i%!eat(); // 1
i* (a$i% instanceof 0ol*)
((0ol*)a$i%)!o)l(); // 2
i* (a$i% instanceof Sna#e)
((Sna#e)a$i%)!bite(); // 3
}
}
}
inter*ace 3an4igt {
void 5gt();
}

inter*ace 3anS)im {
void s)im();
}

inter*ace 3an4l' {
void 6'();
}

class /ction3aracter {
public void 5gt() {}
}

class 7ero e8tends /ction3aracter implements 3an4igt, 3anS)im, 3an4l' {
public void s)im() {}

public void 6'() {}
}

public class /dventure {
static void t(3an4igt 8) {
8!5gt();
}

static void u(3anS)im 8) {
8!s)im();
}

static void v(3an4l' 8) {
8!6'();
}

static void )(/ction3aracter 8) {
8!5gt();
}

public static void main(String$% args) {

7ero = ne) 7ero();

t(); // %reat it as a &an'ig(t

u(); // %reat it as a &anS$im

v(); // %reat it as a &an'l#

)(); // %reat it as an Action&(aracter
}
}
class 9uter {
class :nner {
private int i;
public :nner (int i) {
tis!i = i;
}
public int value () {
return i;
}
}

public :nner get:nner:nstance () {
:nner in = ne) :nner (..);
return in;
}
}

public class 1est {
public static void main(String$% args) {
9uter out = ne) 9uter ();

9uter!:nner in. = out!get:nner:nstance();
9uter!:nner in2 = out!ne) :nner(.+);

S'stem!out!println(in.!value());
S'stem!out!println(in2!value());
}}
inter*ace 7idden {
public int value();
}

class 9uter {
private class 7idden:nner implements 7idden {
private int i;
public 7idden:nner (int i) {
tis!i = i;
}
public int value () {
return i;
}
}

public 7idden get:nner:nstance () {
7idden:nner in = ne) 7idden:nner(..);
return in;
}
}

public class 1est {
public static void main(String$% args) {
9uter out = ne) 9uter();

9uter!7idden:nner in. = out!get:nner:nstance(); // va genera eroare, tipul
)uter*+idden,nner nu este vi-i!il
9uter!7idden:nner in2 = ne) 9uter()!ne) 7idden:nner(.+); // din nou eroare

7idden in; = out!get:nner:nstance(); // acces corect la o instanta
+idden,nner
S'stem!out!println(in;!value());
}
}
inter*ace 7idden {
public int value ();
}

class 9uter {
public 7idden get:nner:nstance(int i) {
i* (i == ..) {
class "loc#:nner implements 7idden {
private int i = ..;

public int value() {
return i;
}
}

return ne) "loc#:nner();
}

return null;
}
}
inter*ace 7idden {
public int value();
}

class 9uter {
public 7idden get:nner:nstance(int i) {
return ne) 7idden() {
private int i = ..;
public int value() {
return i;
}
};
}
}

public class 1est {
public static void main(String$% args) {
9uter out = ne) 9uter();

7idden in; = out!get:nner:nstance(..);
S'stem!out!println(in;!value());
}
}
class 9uter {

public int outer<ember = =;

class NonStatic:nner {
private int i = .;
public int value() {
return i - 9uter!tis!outer<ember; // )., putem accesa un mem!ru al clasei
e/terioare
}
}

static class Static:nner {
public int # = ==;
public int value() {
# -= outer<ember; // 01)A10, nu putem accesa un mem!ru nestatic al
clasei e/terioare
return #;
}
}
}

public class 1est {

public static void main(String$% args) {
9uter out = ne) 9uter ();

9uter!NonStatic:nner nonSt = out!ne) NonStatic:nner(); // instantiere &)10&%A
pt o clasa nestatica
9uter!Static:nner st = out!ne) Static:nner(); // instantiere ,2&)10&%A a
clasei statice
9uter!Static:nner st2 = ne) 9uter!Static:nner(); // instantiere &)10&%A a
clasei statice
}
}
class 0it:nner {
class :nner {
public void metod() {
S'stem!out!println(": am :nner>s metod");
}
}
}

class :nerit:nner e8tends 0it:nner!:nner {

:nerit:nner() {} // 01)A10, avem nevoie de o legatura la o!iectul clasei
e/terioare
:nerit:nner(0it:nner )i) { // ).
)i!super();
}
}

public class 1est {
public static void main(String$% args) {
0it:nner )i = ne) 0it:nner();
:nerit:nner ii = ne) :nerit:nner()i);
ii!metod();
}
}
3ollection c = ne) /rra'&ist();
:terator it = c!iterator();

)ile (it!asNe8t()) {
//veri3cari asupra elementului curent" it*ne/t()4
it!remove();
}
3ollection collection = ne) /rra'&ist();
*or (9b?ect o @ collection)
S'stem!out!println(o);
class Student {
String name; // numele studentului
6oat avg; // media

public Student(String name, 6oat avg) {
tis!name = name;
tis!avg = avg;
}

public String toString() {
return "$" - name - ", " - avg - "%";
}
}
public class 1est {
public static void main(String$% args) {

<ap,String,StudentA students = ne) 7as<ap,String,StudentA();

students!put("<atei", ne) Student("<atei", B!=+4));
students!put("/ndrei", ne) Student("/ndrei", C!D+4));
students!put("<iai", ne) Student("<iai", =!=+4));

S'stem!out!println(students!get("<iai")); // elementul avand c(eia 5Andrei5
//adaugam un element cu o c(eie e/istenta
S'stem!out!println(students!put("/ndrei", ne) Student("", +!+4)));
//put intoarce vec(iul element,

//si apoi il suprascrie
S'stem!out!println(students!get("/ndrei"));

//remove intoarce elementul sters
S'stem!out!println(students!remove("<atei"));

//a3sare a structurii
S'stem!out!println(students);
}}
*or (<ap!Entr',String, StudentA entr' @ students!entr'Set())
S'stem!out!println("<edia studentului " - entr'!getFe'() - " este " -
entr'!getGalue()!get/verage());
/rra'&ist,:ntegerA m'&ist = ne) /rra'&ist,:ntegerA();
:terator,:ntegerA it = m'&ist!iterator();
&ist,StringA string&ist = ne) /rra'&ist,StringA(); // 1
&ist,9b?ectA ob?ect&ist = string&ist;
void print3ollection(3ollection,HA c) {
*or (9b?ect e @ c)
S'stem!out!println(e);
}
class PiIIa {
protected String name = "PiIIa";

public String getName() {
return name;
}
}

class 7amPiIIa e8tends PiIIa {
public 7amPiIIa() {
name = "7amPiIIa";
}
}

class 3eesePiIIa e8tends PiIIa {
public 3eesePiIIa() {
name = "3eesePiIIa";
}
}

class <'/pplication {
// Aici folosim 5!ounded $ildcards5
public static void listPiIIa(&ist,H e8tends PiIIaA piIIa&ist) {
*or(PiIIa item @ piIIa&ist)
S'stem!out!println(item!getName());
}

public static void main(String$% args) {
&ist,PiIIaA p&ist = ne) /rra'&ist,PiIIaA();

p&ist!add(ne) 7amPiIIa());
p&ist!add(ne) 3eesePiIIa());
p&ist!add(ne) PiIIa());

<'/pplication!listPiIIa(p&ist);
// Se va a3sa" 5+am6i--a5, 5&(eese6i--a5, 56i--a5
}
}
public class Singleton {
private Singleton instance = null;

public static Singleton get:nstance() {
i*(instance == null)
return ne) Singleton();
}
!!!
}
abstract class PiIIa {
public abstract double getPrice();
}
class 7am/nd<usroomPiIIa e8tends PiIIa {
public double getPrice() {
return D!5;
}
}
class Jelu8ePiIIa e8tends PiIIa {
public double getPrice() {
return .+!5;
}
}
class 7a)aiianPiIIa e8tends PiIIa {
public double getPrice() {
return ..!5;
}
}

class PiIIa4actor' {
public enum PiIIa1'pe {
7am<usroom, Jelu8e, 7a)aiian
}
public static PiIIa createPiIIa(PiIIa1'pe piIIa1'pe) {
s)itc (piIIa1'pe) {
case 7am<usroom@ return ne) 7am/nd<usroomPiIIa();
case Jelu8e@ return ne) Jelu8ePiIIa();
case 7a)aiian@ return ne) 7a)aiianPiIIa();
}
tro) ne) :llegal/rgumentE8ception("1e piIIa t'pe " - piIIa1'pe - " is not
recogniIed!");
}
}
public class PiIIa&over {
public static void main (String args$%) {
*or (PiIIa4actor'!PiIIa1'pe piIIa1'pe @ PiIIa4actor'!PiIIa1'pe!values()) {
S'stem!out!println("Price o* " - piIIa1'pe - " is " -
PiIIa4actor'!createPiIIa(piIIa1'pe)!getPrice());
}
}
}
public class MyStack {
private int maxSize;
private long[] stackArray;
private int top;
public MyStack(int s) {
maxSize = s;
stackArray = ne long[maxSize];
top = !";
#
public voi$ pus%(long &) {
stackArray[''top] = &;
#
public long pop() {
return stackArray[top!!];
#
public long peek() {
return stackArray[top];
#
public boolean is(mpty() {
return (top == !");
#
public boolean is)ull() {
return (top == maxSize ! ");
#
public static voi$ main(String[] args) {
MyStack t%eStack = ne MyStack("*);
t%eStack+pus%("*);
t%eStack+pus%(,*);
t%eStack+pus%(-*);
t%eStack+pus%(.*);
t%eStack+pus%(/*);
%ile (0t%eStack+is(mpty()) {
long value = t%eStack+pop();
System+out+print(value);
System+out+print(1 1);
#
System+out+println(11);
#
#
import &ava+util+2inke$2ist;
class 3en4ueue5(6 {
private 2inke$2ist5(6 list = ne 2inke$2ist5(6();
public voi$ en7ueue(( item) {
list+a$$2ast(item);
#
public ( $e7ueue() {
return list+poll();
#
public boolean %as8tems() {
return 0list+is(mpty();
#
public int size() {
return list+size();
#
public voi$ a$$8tems(3en4ueue59 exten$s (6 7) {
%ile (7+%as8tems())
list+a$$2ast(7+$e7ueue());
#
#
public class 3en4ueue:est {
public static voi$ main(String[] args) {
3en4ueue5(mployee6 emp2ist;
emp2ist = ne 3en4ueue5(mployee6();
3en4ueue5;ourly(mployee6 %2ist;
%2ist = ne 3en4ueue5;ourly(mployee6();
%2ist+en7ueue(ne ;ourly(mployee(1:1< 1=1));
%2ist+en7ueue(ne ;ourly(mployee(131< 1>1));
%2ist+en7ueue(ne ;ourly(mployee(1)1< 1S1));
emp2ist+a$$8tems(%2ist);
System+out+println(1:%e employees? names are@1);
%ile (emp2ist+%as8tems()) {
(mployee emp = emp2ist+$e7ueue();
System+out+println(emp+AirstBame ' 1 1
' emp+lastBame);
#
#
#
import &ava+util+Scanner;

class >inarySearc%
{
public static void main(String args[])
{
int c< Airst< last< mi$$le< n< searc%< array[];

Scanner in = new Scanner(System+in);
System+out+println(1(nter number oA elements1);
n = in+next8nt();
array = new int[n];

System+out+println(1(nter 1 ' n ' 1 integers1);


for (c = *; c 5 n; c'')
array[c] = in+next8nt();

System+out+println(1(nter value to Ain$1);
searc% = in+next8nt();

Airst = *;
last = n ! ";
mi$$le = (Airst ' last)C,;

while( Airst 5= last )
{
if ( array[mi$$le] 5 searc% )
Airst = mi$$le ' ";
else if ( array[mi$$le] == searc% )
{
System+out+println(searc% ' 1 Aoun$ at location 1 ' (mi$$le ' ") '
1+1);
break;
#
else
last = mi$$le ! ";

mi$$le = (Airst ' last)C,;
#
if ( Airst 6 last )
System+out+println(searc% ' 1 is not present in t%e list+\n1);
#
#
public class >S:5Dey exten$s Eomparable5Dey6< Falue6 {
private Bo$e root; // root of BST
private class Bo$e {
private Dey key; // sorted by key
private Falue val; // associated data
private Bo$e leAt< rig%t; // left and right subtrees
private int B; // number of nodes in subtree
public Node(Dey key< Falue val< int B) {
t%is+key = key;
t%is+val = val;
t%is+B = B;
#
#
// is the symbol table empty?
public boolean isEmpty() {
return size() == *;
#
// return number of key-value pairs in BST
public int size() {
return size(root);
#
// return number of key-value pairs in BST rooted at x
private int size(Bo$e x) {
iA (x == null) return *;
else return x+B;
#
/***********************************************************************
* Search BST for given key, and return associated value if found,
* return null if not found
***********************************************************************/
// does there exist a key-value pair ith given key?
public boolean contains(Dey key) {
return get(key) 0= null;
#
// return value associated ith the given key, or null if no such key
exists
public Falue get(Dey key) {
return get(root< key);
#
private Falue get(Bo$e x< Dey key) {
iA (x == null) return null;
int cmp = key+compareTo(x+key);
iA (cmp 5 *) return get(x+leAt< key);
else iA (cmp 6 *) return get(x+rig%t< key);
else return x+val;
#
/***********************************************************************
* !nsert key-value pair into BST
* !f key already exists, update ith ne value
***********************************************************************/
public voi$ put(Dey key< Falue val) {
iA (val == null) { delete(key); return; #
root = put(root< key< val);
assert check();
#
private Bo$e put(Bo$e x< Dey key< Falue val) {
iA (x == null) return ne Node(key< val< ");
int cmp = key+compareTo(x+key);
iA (cmp 5 *) x+leAt = put(x+leAt< key< val);
else iA (cmp 6 *) x+rig%t = put(x+rig%t< key< val);
else x+val = val;
x+B = " ' size(x+leAt) ' size(x+rig%t);
return x;
#
/***********************************************************************
* "elete
***********************************************************************/
public voi$ deleteMin() {
iA (isEmpty()) t%ro ne NoSuchElementException(1Symbol table
un$erAlo1);
root = deleteMin(root);
assert check();
#
private Bo$e deleteMin(Bo$e x) {
iA (x+leAt == null) return x+rig%t;
x+leAt = deleteMin(x+leAt);
x+B = size(x+leAt) ' size(x+rig%t) ' ";
return x;
#
public voi$ deleteMax() {
iA (isEmpty()) t%ro ne NoSuchElementException(1Symbol table
un$erAlo1);
root = deleteMax(root);
assert check();
#
private Bo$e deleteMax(Bo$e x) {
iA (x+rig%t == null) return x+leAt;
x+rig%t = deleteMax(x+rig%t);
x+B = size(x+leAt) ' size(x+rig%t) ' ";
return x;
#
public voi$ delete(Dey key) {
root = delete(root< key);
assert check();
#
private Bo$e delete(Bo$e x< Dey key) {
iA (x == null) return null;
int cmp = key+compareTo(x+key);
iA (cmp 5 *) x+leAt = delete(x+leAt< key);
else iA (cmp 6 *) x+rig%t = delete(x+rig%t< key);
else {
iA (x+rig%t == null) return x+leAt;
iA (x+leAt == null) return x+rig%t;
Bo$e t = x;
x = min(t+rig%t);
x+rig%t = deleteMin(t+rig%t);
x+leAt = t+leAt;
#
x+B = size(x+leAt) ' size(x+rig%t) ' ";
return x;
#
public boolean e7uals(Gb&ect ot%er){
if (ot%er == null){
return false;
#
if(this+getElass() 0= ot%er+getElass()){
return false;
#
if(this+nota 0= ((Stu$ent)ot%er)+nota){
return false;
#
if(this+nume 0= ((Stu$ent)ot%er)+nume){
return false;
#
return true;
#
public class Stu$ent implements Eomparable5Gb&ect6{
public String name;
public float me$ie;
public Stu$ent(String name< float me$ie){
this+name=name;
this+me$ie=me$ie;
#
public String toString(){
return name ' 1 1 ' me$ie;
#
HGverri$e
public boolean e7uals(Gb&ect o) {
System+out+println(1e7"1);
if (o == null) {
return false;
#
if ( 0(o instanceof Stu$ent) ) {
return false;
#
if (this+name 0= ((Stu$ent) o)+name) {
return false;
#
if (this+me$ie 0= ((Stu$ent) o)+me$ie) {
return false;
#
return true;
#
public boolean e7uals(Stu$ent s){
System+out+println(1e7,1);
return false;
#
HGverri$e
public int %as%Eo$e(){
final int prim = -";
int result = ";
result = result I prim ' this+name+%as%Eo$e();
result = result I prim ' (int)this+me$ie;
return result;
#
public float getMe$(){
return me$ie;
#
public String getBame(){
return name;
#
HGverri$e
public int compare:o(Gb&ect ob&) {
return this+name+compare:o(((Stu$ent)ob&)+name);
#

You might also like