id stringlengths 36 36 | text stringlengths 1 1.25M |
|---|---|
c222a1ac-a39f-4bbf-bb90-4ca20c68f65a | public void print(); |
71692511-1b89-4783-8d61-f5bb72e0f100 | @Test
public void simpleBinarySearch() {
BinarySearch binary = new BinarySearch();
int[] array = { 10, 4, 5, 2, 3, 6, 8, 7, 9, 1 };
Arrays.sort(array);
Assert.assertEquals(binary.contains(array, 6), 6);
Assert.assertEquals(binary.contains(array, 10), 10);
Assert.assertEquals(binary.contains(array, 5), 5);
Assert.assertEquals(binary.contains(array, 11), -1);
} |
578dab4e-bb5a-42f8-aa6b-cd2fde984b3f | @Test
public void recursiveBinarySearch() {
BinarySearch binary = new BinarySearch();
int[] array = { 10, 4, 5, 2, 3, 6, 8, 7, 9, 1 };
Arrays.sort(array);
int first = 0;
int last = array.length;
Assert.assertEquals(binary.recursiveBinarySearch(array, first, last, 6), 6);
Assert.assertEquals(binary.recursiveBinarySearch(array, first, last, 10), 10);
Assert.assertEquals(binary.recursiveBinarySearch(array, first, last, 5), 5);
Assert.assertEquals(binary.recursiveBinarySearch(array, first, last, 11), -1);
} |
bf05c2cc-83fe-42ad-9df4-07db34ddc932 | @Test
public void testSelectionSort() {
int testCase[] = { 2, 1, 5, 1, 2, 3, 4, 3, 5, 6, 7, 8, 5, 6, 7, 0 };
int expected[] = { 0, 1, 1, 2, 2, 3, 3, 4, 5, 5, 5, 6, 6, 7, 7, 8 };
SelectionSort sorter = new SelectionSort();
Assert.assertArrayEquals(expected, sorter.selectionSort1(testCase));
Assert.assertArrayEquals(expected, sorter.selectionSort2(testCase));
Assert.assertArrayEquals(expected, sorter.shortSelectionSort(testCase));
} |
5b13a4f3-3708-4843-8340-26e95e40f509 | @Test
public void findSingleOdd() {
int[] arr = {1,2,3,4,5,6,1,2,3,4,5};
XOR xor = new XOR();
Assert.assertEquals(6, xor.findSingleOdd(arr));
} |
c2b24740-cac9-4e01-9116-bbde28d6908b | @Test
public void testBucketSort() {
int[] arr = { 2, 1, 5, 1, 2, 3, 4, 3, 5, 6, 7, 8, 5, 6, 7, 0 };
int[] expected = { 0, 1, 1, 2, 2, 3, 3, 4, 5, 5, 5, 6, 6, 7, 7, 8 };
BucketSort sort = new BucketSort();
Assert.assertArrayEquals(expected, sort.sort(arr, 0, 8));
} |
a79665f4-0503-4735-8326-e370021cd2e0 | @Test
public void testBubbleSort() {
int[] arr = {55, 33,7, 2, 45, 76, 23, 56, 87, 2, 23, 56, 22};
BubbleSort sort = new BubbleSort();
int[] expected = {2, 2, 7, 22, 23, 23, 33, 45, 55, 56, 56, 76, 87};
Assert.assertArrayEquals(expected, sort.bubbleSort(arr));
} |
ec1b453b-7807-4b15-ada0-ca9cbf8a7c35 | public static void main(String[] args) {
System.out.println("Digite alguma coisa");
Scanner scanner = new Scanner(System.in);
String texto = scanner.nextLine();
System.out.println(texto);
} |
fba43bb8-c199-4ec3-bd94-020aad8418bc | public void trabalhar() {
funcionario.desenvolver();
} |
a74da366-eec8-46c5-b1cd-259c8019a47e | @Override
public void desenvolver() {
System.out.println("Fun1.. desenvolvendo..");
} |
0d4d5cfb-7be2-4167-9286-1357b2623a16 | public void trampando() {
System.out.println(nome + " trabalhando..");
} |
2264c33a-2536-423e-9961-0003b905f777 | public static void main(String[] args) {
Funcionario f1 = new Funcionario();
Funcionario f2 = new Funcionario();
Pessoa p = new Pessoa();
p.nome = "Fernando";
Empresa e = new Empresa();
e.funcionario = f1;
e.trabalhar();
AdapterPessoa ap = new AdapterPessoa(p);
e.funcionario = ap;
e.trabalhar();
} |
98336f0b-20e3-483c-8462-5c8c3f9c462c | void desenvolver(); |
dc341eda-ab87-4e31-8871-6bec7c48144a | public AdapterPessoa(Pessoa p) {
this.pessoa = p;
} |
1ca55394-d1c1-4f27-8321-2ec48a864b3b | @Override
public void desenvolver() {
pessoa.trampando();
} |
97db641e-22a1-4a76-a722-30c5c4aa5ede | public static void main(String[] args) {
String[] a = { "FERNANDO", "CARLA", "JOAO", "CARLA", "FERNANDO" };
List<String> asList = Arrays.asList(a);
System.out.println(asList);
Set<String> set = new HashSet<>(asList);
System.out.println(set);
} |
fccf8231-ce94-40e1-a12b-b9adb67e2a71 | public PrismMSP(int i) {
MST = new boolean[i];
minDist = new int[i];
graph = new int[i][i];
values=new int [i];
} |
805e0f87-15c1-4bd7-a05c-deb90c19f564 | void createGraph(int src, int dest, int dist) {
graph[src][dest] = dist;
graph[dest][src] = dist;
} |
d229488a-7066-47b9-8ce1-b61a4513afe1 | void create(){
graph= new int[][]{{0, 2, 0, 6, 0},
{2, 0, 3, 8, 5},
{0, 3, 0, 0, 7},
{6, 8, 0, 0, 9},
{0, 5, 7, 9, 0},
};
} |
8233513a-0165-4e8d-8b80-b5524682c34a | void defaultGraphValues() {
for (int i = 0; i < graph.length; i++) {
for (int j = 0; j < graph.length; j++) {
graph[i][j] = Integer.MAX_VALUE;
}
}
} |
3b4d59a9-9af7-4b4e-8ca3-ad90f0760f04 | void defaultValue() {
for (int i = 0; i < graph.length; i++) {
minDist[i]=Integer.MAX_VALUE;
}
} |
9cf29614-00b2-41f5-afbb-80e9ef504412 | int findMinValue(){
int minIndex = 0;
int prevValue=Integer.MAX_VALUE;
for(int i=0;i<graph.length;i++){
if(minDist[i]<prevValue && !MST[i] && prevValue!=0) {
minIndex = i;
prevValue=minDist[i];
}
}
return minIndex;
} |
51bd6394-7cbe-456e-a8fa-add725ac3b85 | void MSP(){
// MST[i-1]=true;
// defaultGraphValues();
defaultValue();
minDist[0]=-1;
for(int j=0;j<graph.length;j++){
int u=findMinValue();
System.out.println("MIN INDEX:"+u);
MST[u]=true;
for(int k=0;k<graph.length;k++){
if(graph[u][k]!=0 &&! MST[k] && graph[u][k]<minDist[k]){
minDist[k]=graph[u][k];
values[k]=u;
System.out.println("ADJACENCY"+graph[u][k]);
}
}
}
} |
b34d21d9-8761-4aee-8650-cac77449063f | void print(){
for(int i=1;i<graph.length;i++){
System.out.println(values[i]+"-"+i+"----"+graph[i][values[i]]);
}
} |
a62c12c1-0b72-4e1d-bd8d-04b7c3356e11 | public static void main(String arg[]){
PrismMSP prismMSP=new PrismMSP(5);
prismMSP.create();
prismMSP.MSP();
prismMSP.print();
} |
ff405d0b-a1a6-4f43-b1a1-246cd477e596 | public HeapSortMinHeapOperation(int[] nodeList, int size){
this.nodeList=nodeList;
this.heapSize=size;
} |
ab0c9131-ffa7-44d8-9efe-a2bf5b591015 | void MIN_HEAPIFFY( int[] nodeList,int i){
int l=getLeft(i);
int r=getRight(i);
int largest;
if(l>=0 && l<=heapSize-1 && nodeList[l]<nodeList[i]){
largest= l;
}else{
largest= i;
}
if(r>=0 && r<=heapSize-1 && nodeList[r]<nodeList[largest]){
largest= r;
}
if(largest!=i){
int temp=nodeList[largest];
nodeList[largest]=nodeList[i];
nodeList[i]=temp;
MIN_HEAPIFFY(nodeList, largest);
}
} |
15dbb117-7dc7-40ee-b9f9-836549c6ce24 | void BUILD_MIN_HEAP(int[] nodeList){
for(int i=((heapSize)/2);i>=0;i--) {
MIN_HEAPIFFY(nodeList,i);
}
} |
2ca8b1bc-19be-4115-9a65-8bd22dd18d7b | void HEAP_SORT(int[] nodeList){
BUILD_MIN_HEAP(nodeList);
for(int i=nodeList.length-1;i>=1;i--) {
int temp=nodeList[0];
nodeList[0]=nodeList[i];
heapSize--;
nodeList[i]=temp;
MIN_HEAPIFFY(nodeList, 0);
}
} |
a7942eb7-2dba-4567-872a-591ce756f0ca | int getParent(int i){
return i/2-1;
} |
13f2c924-cbaf-4062-805c-39e3784f3c25 | int getLeft(int i){
return 2*i+1 ;
} |
2960f509-dce2-4da1-983d-e616c1ebec8f | int getRight(int i){
return 2*i+2;
} |
2ca441f3-b91c-4db7-b7b0-1b249750e72a | public static void main(String a[]){
int []arr= {4,5,6,7,8,9,0};
HeapSortMinHeapOperation hp=new HeapSortMinHeapOperation(arr,7);
hp.HEAP_SORT(hp.nodeList);
System.out.println("OUTPUT");
for(int i=0;i<=hp.nodeList.length-1;i++) {
System.out.print(hp.nodeList[i]);
}
} |
e2a720d0-84ce-4306-b2c7-7c68d755daa6 | public MINIMUM_PRIORITY_QUEUE(int[] nodeList, int size){
this.nodeList=nodeList;
this.heapSize=size;
} |
056373e6-8ec0-41cc-bb73-8f13181d6db1 | void MIN_HEAPIFFY(int i){
int l=getLeft(i);
int r=getRight(i);
int largest;
if(l>=0 && l<=heapSize-1 && nodeList[l]<nodeList[i]){
largest= l;
}else{
largest= i;
}
if(r>=0 && r<=heapSize-1 && nodeList[r]<nodeList[largest]){
largest= r;
}
if(largest!=i){
int temp=nodeList[largest];
nodeList[largest]=nodeList[i];
nodeList[i]=temp;
MIN_HEAPIFFY( largest);
}
} |
78100836-519f-452a-91ad-209f6dfca259 | void BUILD_MIN_HEAP() {
for (int i = ((heapSize) / 2); i >= 0; i--) {
MIN_HEAPIFFY( i);
}
} |
fd643399-13d0-4e5b-b022-cc9f6ff4e2bb | int getParent(int i){
return (i-1)/2;
} |
7cf6f4fb-d867-43f9-b704-42b8604f5642 | int getLeft(int i){
return 2*i+1 ;
} |
db814a19-25a2-4ccd-bc10-df2753938452 | int getRight(int i){
return 2*i+2;
} |
bda89a5c-fcbf-4f12-a56f-d5035b73b5ec | int EXTRACT_MIN() throws Exception {
if(heapSize<0){
throw new Exception("Heap Underflow");
}
int max=nodeList[0];
nodeList[0]=nodeList[heapSize-1];
heapSize--;
MIN_HEAPIFFY(0);
return max;
} |
b96af65e-80e0-4d90-a62e-df988df4b26f | void HEAP_DECREASE_KEY(int pos,int value) throws Exception {
if(nodeList[pos]<value){
throw new Exception("Heap Underflow");
}
nodeList[pos]=value;
while(pos>0 && nodeList[getParent(pos)]> nodeList[pos]){
int temp=nodeList[getParent(pos)];
nodeList[getParent(pos)]=nodeList[pos];
nodeList[pos]=temp;
pos=getParent(pos);
}
} |
a768eec8-33bb-4e21-a73b-c7bab2ef68b4 | public static void main(String a[]) throws Exception {
int []arr= {4,5,6,7,8,9,0};
MINIMUM_PRIORITY_QUEUE hp=new MINIMUM_PRIORITY_QUEUE(arr,7);
hp.BUILD_MIN_HEAP();
System.out.println("AFTER EXTRACTING MIN KEY:");
for(int i=0;i<=hp.heapSize-1;i++) {
System.out.print(hp.nodeList[i]);
}
System.out.println("EXTRACTED MIN KEY:"+hp.EXTRACT_MIN());
System.out.println("AFTER EXTRACTING MIN KEY:");
for(int i=0;i<=hp.heapSize-1;i++) {
System.out.print(hp.nodeList[i]);
}
System.out.println();
System.out.println("Decreasing KEY");
hp.HEAP_DECREASE_KEY(3,1);
System.out.println("AFTER Decreasing KEY:");
for(int i=0;i<=hp.heapSize-1;i++) {
System.out.print(hp.nodeList[i]);
}
} |
00f8231f-9c38-4cdd-9ff0-e2241a4551fd | public HeapSortMaxHeapOperation(int[] nodeList, int size){
this.nodeList=nodeList;
this.heapSize=size;
} |
04c66b4f-02fe-49f4-ac07-a68cb3496f37 | void MAX_HEAPIFFY( int[] nodeList,int i){
int l=getLeft(i);
int r=getRight(i);
int largest;
if(l>=0 && l<=heapSize-1 && nodeList[l]>nodeList[i]){
largest= l;
}else{
largest= i;
}
if(r>=0 && r<=heapSize-1 && nodeList[r]>nodeList[largest]){
largest= r;
}
if(largest!=i){
int temp=nodeList[largest];
nodeList[largest]=nodeList[i];
nodeList[i]=temp;
MAX_HEAPIFFY(nodeList, largest);
}
} |
224563c9-b491-447e-a25c-7472f0608fcd | void BUILD_MAX_HEAP(int[] nodeList){
for(int i=((heapSize)/2);i>=0;i--) {
MAX_HEAPIFFY(nodeList,i);
}
} |
7478ef30-99b3-4a4f-8fc9-63943d221e58 | void HEAP_SORT(int[] nodeList){
BUILD_MAX_HEAP(nodeList);
for(int i=nodeList.length-1;i>=1;i--) {
int temp=nodeList[0];
nodeList[0]=nodeList[i];
heapSize--;
nodeList[i]=temp;
MAX_HEAPIFFY(nodeList, 0);
}
} |
7be5a0bd-0d23-4c38-a840-e39ae7d51d11 | int getParent(int i){
return i/2-1;
} |
eec28bc5-63ae-486a-b615-d65bb5040079 | int getLeft(int i){
return 2*i+1 ;
} |
f44ef277-7e26-4551-b802-9f8dbb2efd2b | int getRight(int i){
return 2*i+2;
} |
2e42a01b-9a95-4d93-9c8e-8d91890b3ab8 | public static void main(String a[]){
int []arr= {4,5,6,7,8,9,0};
HeapSortMaxHeapOperation hp=new HeapSortMaxHeapOperation(arr,7);
hp.HEAP_SORT(hp.nodeList);
System.out.println("OUTPUT");
for(int i=0;i<=hp.nodeList.length-1;i++) {
System.out.print(hp.nodeList[i]);
}
} |
a7f3beac-63d0-497d-b3ad-69cd51631905 | public Node(int s,int d,int w,Node nextRef){
this.source=s;
this.destination=d;
this.weight=w;
} |
e59e2e72-6c16-4623-8433-af177bd61767 | public MAXIMUM_PRIORITY_QUEUE(int[] nodeList, int size){
this.nodeList=nodeList;
this.heapSize=size;
} |
5ea28656-1c4f-4ce8-b72b-b5ee9bf22dac | void MAX_HEAPIFFY(int i){
int l=getLeft(i);
int r=getRight(i);
int largest;
if(l>=0 && l<=heapSize-1 && nodeList[l]>nodeList[i]){
largest= l;
}else{
largest= i;
}
if(r>=0 && r<=heapSize-1 && nodeList[r]>nodeList[largest]){
largest= r;
}
if(largest!=i){
int temp=nodeList[largest];
nodeList[largest]=nodeList[i];
nodeList[i]=temp;
MAX_HEAPIFFY( largest);
}
} |
87dae82c-ccf1-41a3-b969-f16112eda40c | void BUILD_MAX_HEAP() {
for (int i = ((heapSize) / 2); i >= 0; i--) {
MAX_HEAPIFFY( i);
}
} |
6f8b8342-0faa-45d3-b946-6f04aa812fa3 | int getParent(int i){
return (i-1)/2;
} |
4e85dc7c-1fc0-4e1f-9690-e4cb23e6f0a0 | int getLeft(int i){
return 2*i+1 ;
} |
5af0432a-ffe9-4afb-b639-87e0f582d75f | int getRight(int i){
return 2*i+2;
} |
3dff0929-f8b9-4c85-9872-dce679cfae55 | int EXTRACT_MAX() throws Exception {
if(heapSize<0){
throw new Exception("Heap Underflow");
}
int max=nodeList[0];
nodeList[0]=nodeList[heapSize-1];
heapSize--;
MAX_HEAPIFFY(0);
return max;
} |
2178fb28-7a71-4258-829c-b16017d33ec1 | void HEAP_INCREASE_KEY(int pos,int value) throws Exception {
if(nodeList[pos]>value){
throw new Exception("Heap Underflow");
}
nodeList[pos]=value;
while(pos>0 && nodeList[getParent(pos)]< nodeList[pos]){
int temp=nodeList[getParent(pos)];
nodeList[getParent(pos)]=nodeList[pos];
nodeList[pos]=temp;
pos=getParent(pos);
}
} |
749ed8ef-3a4c-4244-affa-d80782390cb2 | void MAX_HEAP_INSERT(int keyValue) throws Exception {
if(heapSize==nodeList.length){
Arrays.copyOf(nodeList, nodeList.length+nodeList.length>>1);
}
heapSize++;
nodeList[heapSize-1]=Integer.MIN_VALUE;
HEAP_INCREASE_KEY(heapSize-1,keyValue);
} |
ec36d8c3-8d32-497d-a70a-230bf9b584dc | public static void main(String a[]) throws Exception {
int []arr= {4,5,6,7,8,9,0};
MAXIMUM_PRIORITY_QUEUE hp=new MAXIMUM_PRIORITY_QUEUE(arr,7);
hp.BUILD_MAX_HEAP();
System.out.println("EXTRACTED MAX KEY:"+hp.EXTRACT_MAX());
System.out.println("AFTER EXTRACTING MAX KEY:");
for(int i=0;i<=hp.heapSize-1;i++) {
System.out.print(hp.nodeList[i]);
}
System.out.println();
System.out.println("Increasing KEY");
hp.HEAP_INCREASE_KEY(3,15);
System.out.println("AFTER Increasing KEY:");
for(int i=0;i<=hp.heapSize-1;i++) {
System.out.print(hp.nodeList[i]);
}
System.out.println();
System.out.println("Inserting KEY");
hp.MAX_HEAP_INSERT(16);
System.out.println("AFTER Inserting KEY:");
for(int i=0;i<=hp.heapSize-1;i++) {
System.out.print(hp.nodeList[i]);
}
System.out.println();
System.out.println("EXTRACTED MAX KEY:"+hp.EXTRACT_MAX());
System.out.println("AFTER EXTRACTING MAX KEY:");
for(int i=0;i<=hp.heapSize-1;i++) {
System.out.print(hp.nodeList[i]);
}
} |
8dda76ae-94c5-4283-aec1-8e398e6fad7c | public UnionFindAlgorithm(int size){
arr=new int[size];
for(int i=0;i<size;i++){
arr[i]=i;
}
} |
1002f0be-40c3-4b44-8429-44b96d50c5d6 | int find(int x){
if(arr[x]==x)
return x;
arr[x]=find(arr[x]);
return arr[x];
} |
12300759-99de-4817-84b4-fc19d37861fd | void union(int x,int y){
arr[find(x)]=arr[find(y)];
} |
1f8e51f1-1ee7-460a-b23c-c62aa9839dea | void perfomFindAndUnionOnVertices(int x,int y){
int tempX=find(x);
int tempY=find(y);
if(tempX!=tempY){
union(x,y);
System.out.println("source:"+x+"Destination:"+y);
}
} |
6cf0d6df-ddfc-460c-85d0-e4543008d159 | void addEdge(int source, int dest, int weight) {
nodeCount++;
if (edges == null) {
edges = new Node(source, dest, weight, null);
} else {
edges = new Node(source, dest, weight, edges);
}
} |
81cdd7d9-dc09-4ee3-9779-44eadb4a78e2 | void performMST(){
UnionFindAlgorithm uf=new UnionFindAlgorithm(nodeCount);
MergeSort mg=new MergeSort();
Node head=null;
head=mg.merge(edges);
while(head!=null){
uf.perfomFindAndUnionOnVertices(head.source,head.destination);
head=head.nextRef;
}
} |
8341a401-d325-4c68-abfb-5b40b8ef0237 | public static void main(String[] args) {
KruskalMST mst=new KruskalMST();
mst.addEdge(0,1,3);
mst.addEdge(1,4,3);
mst.addEdge(1,2,5);
mst.addEdge(0,2,6);
mst.addEdge(2,3,1);
mst.addEdge(3,0,2);
mst.performMST();
} |
8ea9e118-d9f2-4cce-9e69-b855befae6b3 | public Node(int s,int d,int w,Node nextRef){
this.source=s;
this.destination=d;
this.weight=w;
this.nextRef=nextRef;
} |
3d2e9e06-d71a-47a8-ad21-4fbc334eae18 | Node merge(Node source){
if(source ==null ||source.nextRef==null)
return source;
Node middle=middleOfLS(source);
Node temp=middle.nextRef;
middle.nextRef=null;
System.out.println("middle:"+middle.source);
Node h1= merge(source);
Node h2=merge(temp);
return mergeSort(h1,h2);
} |
f02406b9-6e99-4de7-9b59-78f4add488f0 | Node middleOfLS(Node source){
Node temp=source;
Node temp1=source;
while(temp1.nextRef!=null && temp1.nextRef.nextRef!=null ){
temp=temp.nextRef;
temp1=temp1.nextRef.nextRef;
}
return temp;
} |
9eff5130-6dcc-4f15-aecf-873a580c8b74 | Node mergeSort(Node temp,Node temp1){
Node result=null;
if(temp==null)
return temp1;
if(temp1==null)
return temp;
if(temp.weight<=temp1.weight){
result=temp;
result.nextRef=mergeSort(temp.nextRef,temp1);
}else{
result=temp1;
result.nextRef=mergeSort(temp,temp1.nextRef);
}
return result;
} |
20720403-3a2d-44dd-9f90-6f44a906a732 | public static void main(String[] args) {
Controller app = new Controller();
app.run();
} |
5ae021ef-cc50-450d-80d0-a2c5f64f6dc7 | public void addData(String buttonText) {
int type = findDataType(buttonText);
sequence.add(new Data(buttonText, type));
} |
e09e7366-e7e8-48df-b4cf-416b6f39b271 | public ArrayList<Data> getSequence() {
return sequence;
} |
e40d8aea-2eec-4a93-aae1-d691470fbbc6 | public void reset() {
sequence.clear();
} |
d5002f23-8dc6-4bd9-820d-ab79cb62e758 | @Data.DataType
public int findDataType(String buttonText) {
int returnValue;
switch (buttonText) {
case "+":
case "-":
case "*":
case "/":
returnValue = Data.TYPE_OPERATOR;
break;
case "=":
case "CE":
case "C":
returnValue = Data.TYPE_COMMAND;
break;
default:
returnValue = Data.TYPE_INPUT;
break;
}
return returnValue;
} |
e9dce7c4-c389-459e-82d3-5117c5aac335 | public String calculateResult() {
BigDecimal firstValue = new BigDecimal(sequence.get(sequence.size() - 3).getValue());
String operator = sequence.get(sequence.size() - 2).getValue();
BigDecimal secondValue = new BigDecimal(sequence.get(sequence.size() - 1).getValue());
BigDecimal result = firstValue;
switch (operator) {
case "+":
result = firstValue.add(secondValue);
break;
case "-":
result = firstValue.subtract(secondValue);
break;
case "*":
result = firstValue.multiply(secondValue);
break;
case "/":
result = firstValue.divide(secondValue);
break;
}
addData(result.toString());
return getLastInput();
} |
71d83eca-e320-4c34-846f-b984c3e816b4 | public String getLastInput() {
return sequence.get(sequence.size() - 1).getValue();
} |
ee130618-27fd-46f8-9275-87c826d72d49 | public void resetLastInput() {
editLatestInput("0");
} |
07ab494c-049e-43e8-b243-f971b4b88ca7 | public void editLatestInput(String text) {
int latestIndex = sequence.size() -1;
Data data = sequence.get(latestIndex);
data.setValue(text);
sequence.set(latestIndex, data);
} |
b3d90da3-3d42-4df5-a21d-22fdfa450ac6 | public Data(String value, @DataType int type) {
this.value = value;
this.type = type;
} |
a466a5aa-8bc0-4744-8151-2e579b9819a2 | public String getValue() {
return value;
} |
209b67cf-3de1-4dec-9ddd-caac9eea45ff | public void setValue(String value) {
this.value = value;
} |
5b2215c5-69bf-48d7-a93f-d460b3622848 | public int getType() {
return type;
} |
fc19ac37-72ca-4362-9cb1-236d7b6bde81 | public void open(Controller controller) {
this.controller = controller;
init();
setVisible(true);
} |
0fead53a-f969-4ab6-9d00-71a345ad4204 | private void init() {
setTitle("Calculator");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
setResizable(ALLOW_RESIZE);
setLocationRelativeTo(null);
// Prepare UI
JPanel panel = new JPanel();
this.getContentPane().add(panel);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
// result text UI
JPanel resultPanel = new JPanel();
resultPanel.setPreferredSize(new Dimension(WINDOW_WIDTH, 50));
resultText = new JTextField();
resultText.setEditable(false);
resultText.setPreferredSize(new Dimension(WINDOW_WIDTH, 50));
resultText.setBackground(Color.BLACK);
resultText.setForeground(Color.green);
resultText.setText("0");
resultText.setHorizontalAlignment(SwingConstants.RIGHT);
resultText.setFont(new Font(null, Font.PLAIN, 23));
resultPanel.add(resultText);
panel.add(resultPanel);
// action Panel
JPanel actionPanel = new JPanel();
actionPanel.setLayout(new BoxLayout(actionPanel, BoxLayout.X_AXIS));
panel.add(actionPanel);
// Number Panel
JPanel numberPanel = new JPanel(new GridLayout(4, 3));
numberPanel.setPreferredSize(new Dimension(250, 350));
numberPanel.setSize(250, 350);
actionPanel.add(numberPanel);
ArrayList<String> listButton = new ArrayList<>();
listButton.add("7");
listButton.add("8");
listButton.add("9");
listButton.add("4");
listButton.add("5");
listButton.add("6");
listButton.add("1");
listButton.add("2");
listButton.add("3");
listButton.add("C");
listButton.add("0");
listButton.add("CE");
for(String buttonName : listButton) {
JButton button = new JButton(buttonName);
button.addActionListener(controller.actionListener());
numberPanel.add(button);
}
// Operator Panel
JPanel operatorPanel = new JPanel();
operatorPanel.setLayout(new BoxLayout(operatorPanel, BoxLayout.Y_AXIS));
operatorPanel.setPreferredSize(new Dimension(150, 350));
operatorPanel.setSize(new Dimension(150, 350));
actionPanel.add(operatorPanel);
ArrayList<String> listOperator = new ArrayList<>();
listOperator.add("+");
listOperator.add("-");
listOperator.add("*");
listOperator.add("/");
listOperator.add("=");
for(String buttonName : listOperator) {
JButton button = new JButton(buttonName);
button.addActionListener(controller.actionListener());
operatorPanel.add(button);
}
} |
7345b39d-ef37-421a-819c-380b45be2c94 | public void setDisplayedText(String text) {
resultText.setText(text);
} |
8603a455-27c2-44ac-8e2c-73b9b13d78ca | public Controller() {
this.calculator = new Calculator();
} |
58fba6dc-7181-4e0a-96e8-b16526aaf6e2 | public void run() {
calculator.addData("0");
view = new View();
view.open(this);
} |
54442551-7fd4-4128-b7d6-5129af341089 | public ActionListener actionListener() {
return new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
String value = button.getText();
// IF debug enabled, write console log
if(Main.DEBUG_MODE) {
System.out.println("Button \"" + value + "\" pressed");
}
if(calculator.findDataType(value) == Calculator.Data.TYPE_OPERATOR) {
calculator.addData(value);
calculator.addData("0");
displayedValue = calculator.getLastInput();
} else if (calculator.findDataType(value) == Calculator.Data.TYPE_COMMAND) {
switch (value) {
case "=":
displayedValue = calculator.calculateResult();
break;
case "C":
calculator.resetLastInput();
displayedValue = "0";
break;
case "CE":
calculator.reset();
displayedValue = "0";
break;
}
} else {
if(displayedValue.equals("0")) {
displayedValue = value;
} else {
displayedValue += value;
}
calculator.editLatestInput(displayedValue);
}
view.setDisplayedText(displayedValue);
}
};
} |
25a8fa94-19c8-4a1f-b54a-b204adac42cc | @Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
String value = button.getText();
// IF debug enabled, write console log
if(Main.DEBUG_MODE) {
System.out.println("Button \"" + value + "\" pressed");
}
if(calculator.findDataType(value) == Calculator.Data.TYPE_OPERATOR) {
calculator.addData(value);
calculator.addData("0");
displayedValue = calculator.getLastInput();
} else if (calculator.findDataType(value) == Calculator.Data.TYPE_COMMAND) {
switch (value) {
case "=":
displayedValue = calculator.calculateResult();
break;
case "C":
calculator.resetLastInput();
displayedValue = "0";
break;
case "CE":
calculator.reset();
displayedValue = "0";
break;
}
} else {
if(displayedValue.equals("0")) {
displayedValue = value;
} else {
displayedValue += value;
}
calculator.editLatestInput(displayedValue);
}
view.setDisplayedText(displayedValue);
} |
5ed6f8f2-a12e-4c8a-b944-6be83f0fef7a | @Before
public void setUp() throws Exception {
model = new Calculator();
} |
10dc9727-3e5b-48b8-9dfa-74f6a3ef8f17 | @After
public void tearDown() throws Exception {
model.reset();
} |
a5c958c0-fcf8-4b3c-936d-915a34a92db1 | @Test
public void testAddData() throws Exception {
// Test input number
String expected = "789";
model.addData(expected);
Calculator.Data data = model.getSequence().get(model.getSequence().size() - 1);
assertEquals("Data value wrong", expected, data.getValue());
assertEquals("Data type wrong", Calculator.Data.TYPE_INPUT, data.getType());
} |
8664a9e1-d241-4624-a640-efdb36eb588e | @Test
public void testReset() throws Exception {
model.addData("123");
model.addData("456");
model.reset();
assertEquals(0, model.getSequence().size());
} |
18120c41-2329-40b9-b1df-9074a284819f | @Test
public void testFindDataType() throws Exception {
assertEquals(Calculator.Data.TYPE_INPUT, model.findDataType("123"));
} |
b45941c2-90de-4e8b-90d3-7734ada25ac8 | @Test
public void testCalculateResult() throws Exception {
model.addData("1");
model.addData("+");
model.addData("3");
assertEquals("4", model.calculateResult());
model.addData("-");
model.addData("3");
assertEquals("1", model.calculateResult());
model.addData("*");
model.addData("4");
assertEquals("4", model.calculateResult());
model.addData("/");
model.addData("2");
assertEquals("2", model.calculateResult());
} |
a807604f-4956-4473-a68f-4406cce8d385 | @Test
public void testGetLatestInput() throws Exception {
model.addData("2");
assertEquals("2", model.getLastInput());
} |
7528219b-02fa-403a-9c2a-cff821e18881 | @Test
public void testResetLatestInput() throws Exception {
model.addData("2");
model.addData("+");
model.addData("3");
model.resetLastInput();
assertEquals("0", model.getLastInput());
assertEquals(3, model.getSequence().size());
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.