explains areas where Supply chain Management (SCM) Systems can be useful in web design.
In: Computer Science
Hi, I don't think my program is calcualting right at all. When I used a text editor to check the # of batches, for 20, it returned 0, but the program is calculated 543 and 1 batch. On 21, there are 29 batches, but the program calculates 28 batches for Chocolate Type #21. I'd like to make sure it calculates everything correctly before I turn it in on Monday.
Here is my program:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
ifstream infile;
ofstream outfile;
//Maximum number of Chocolate Types
const int ARRAY_SIZE = 25;
int printReport(int ChocTypeArray[], int
ChocPiecesArray[])
{
//Opens output file
outfile.open("ChocolateCo_Production_Results.txt");
//Print Title
outfile <<
"======================================================================================================"
<< endl;
outfile << "================================ O-So-Good
Chocolate Company Report =================================="
<< endl;
outfile <<
"======================================================================================================"
<< endl;
//Print Header
outfile << "Chocolate Type" << setw(42) << "
Pieces" << setw(20) << "Batches" << setw(20)
<< "Average/Batch" << endl;
outfile <<
"======================================================================================================"
<< endl;
for (int count = 0; count < ARRAY_SIZE; count++)
{
float avg = 0;
if (ChocTypeArray[count] > 0)
{
avg = (float)ChocTypeArray[count] /
(float)ChocPiecesArray[count];
}
outfile << count + 1 << "\t\t\t\t" <<
right << setw(24) << ChocTypeArray[count] <<
right << setw(20) << ChocPiecesArray[count] <<
right << setw(20) << avg << endl;
}
// Closes outout file
outfile.close();
return 0;
}
int main()
{
int ChocTypeArray[ARRAY_SIZE];
int ChocPiecesArray[ARRAY_SIZE];
//Initialize arrays
for (int count = 0; count < ARRAY_SIZE; count++)
{
ChocTypeArray[count] = 0;
ChocPiecesArray[count] = 0;
}
//Try to find data file
infile.open("Chocolates.txt");
if (!infile)
{
cout << "Failed to open Chocolates.txt. Make sure
it is in the same directory as the program." << endl;
return -1;
}
//Read data until the end of file
int _chocType, _chocPieces;
while (!infile.eof())
{
infile >> _chocType >> _chocPieces;
// Validates if chocType is between 1 and 25
if (_chocType < 1 || _chocType >
ARRAY_SIZE)
{
continue; //skip
}
else
{
//Adjust for array index
_chocType--;
//Add the pieces to their appropriate type
ChocTypeArray[_chocType] =
ChocTypeArray[_chocType] + _chocPieces;
ChocPiecesArray[_chocType]++;
}
}
//Print the report
printReport(ChocTypeArray, ChocPiecesArray);
return 0;
}
Here is the data from Chocolates.txt:
12
1026
15
248
7 402
10
1076
16
948
14
1174
6
1137
8 719
9 952
14
1516
9 830
14
296
19
596
15
874
23
100
15
1063
16
867
6 87
11
1330
1 217
18
802
15
943
15
165
23
100
2 866
21
656
18
885
7
1221
9
1032
22
490
9
1328
12
584
11
561
14
1355
22
750
12
800
9 686
19
401
11
1121
16
389
3 664
8 676
12
692
1 844
1 994
1 847
24
1173
7 792
8
1076
19
613
14
1085
3
1107
12
891
3
1121
6 870
16
300
16
1092
24
969
12
515
6 403
11
885
7 839
14
328
1
1023
7 653
6 276
11
217
3
1288
6
1109
23
643
23
384
7
1213
6 556
4
1224
9
1271
15
1167
8 595
6 746
1
1284
18
1300
14
747
5 650
4 934
9 952
16
1408
24
1012
12
696
14
333
14
478
5 434
1 548
6 285
9 341
23
925
3 150
11
1166
5 963
8
1010
11
1272
1 587
21
866
12
152
11
1280
2 821
4 778
19
1087
15
690
11
1239
6
1267
4 361
12
704
3 560
16
1231
21
1198
6 697
3 696
6 632
16
1543
10
211
2 285
10
845
24
554
16
1105
6
1048
4
1050
16
552
3
1077
19
1333
19
669
8
1172
21
1217
2 826
18
995
7
1140
5
1084
7
1016
21
624
3 654
5 990
9
1115
23
991
3 324
8 959
16
1483
16
1036
9 694
4 817
24
247
6 477
12
1307
10
596
19
1008
1 843
12
1166
24
693
3 397
16
1000
19
368
9
1101
11
1270
15
172
8 839
12
760
6
1099
16
1119
1 673
10
742
2 858
11
1015
8 771
10
537
11
475
6 857
1 932
14
728
11
914
10
652
19
436
2 415
23
724
10
974
2 874
18
365
14
1158
1 986
7 283
21
870
10
708
2 546
7 919
15
429
9 607
19
591
16
938
2 662
6
1044
3 549
5
1012
19
1003
3 654
18
1436
1 738
14
695
14
655
10
266
11
510
6
1265
7 603
24
687
23
1014
9 557
1 677
16
1177
24
712
9
1275
14
1188
16
311
19
581
23
237
21
1228
23
717
24
729
10
511
7 584
23
1043
21
941
23
100
2 966
23
492
5
1312
1 968
18
699
2 249
24
1062
18
1015
2 924
16
952
14
591
4 899
5 692
11
1435
7 760
2 852
15
964
21
921
7
1131
4 919
6 870
4 778
2
1507
14
213
23
713
23
861
1 595
4 623
7 794
7 880
23
1371
9 455
19
1217
16
187
6
1219
10
1135
24
365
18
906
14
576
23
700
19
444
10 11
3 817
19
1394
10
948
7 666
24
883
6
1447
7 385
1 976
9
1037
14
373
2 526
19
1148
16
1185
24
1338
11
854
5
1004
15
1212
8 281
24
1316
6 15
18
1224
19
654
14
483
4
1428
8 769
19
473
14
1185
12
691
5
1252
1 425
6 459
18
1036
15
713
6 840
9 545
12
515
4 599
1 619
6
1162
9 561
5
1105
24
453
15
456
24
644
15
324
18
808
12
860
23
307
18
552
18
568
8 256
16
1458
23
562
19
660
19
983
9 970
7
1154
21
594
9 590
15
174
3
1048
9
1079
3 536
11
1301
16
508
1 810
21
647
10
802
11 50
24
921
18
195
4 273
5 564
11
314
21
710
7 267
15
224
18
215
9 975
9 328
9 475
23
470
12
649
7 782
16
765
16
1019
9 537
4
1089
21
221
15
372
7 706
19
379
10
1015
8
1244
23
443
1 121
6 732
10
718
23
1084
12
424
23
536
11
308
7 637
18
611
19
870
24
310
15
1145
9
1005
10
672
10 59
16
1235
19
764
18
430
12
1122
8 956
23
704
1 507
8 822
3 761
2
1084
11
1066
19
439
15
859
5 494
15
498
5
1092
15
1457
15
458
18
1133
23 44
10
1215
16
856
8 942
1 321
16
663
1 463
5
1324
6 676
7 858
23
536
2 476
24
358
21
481
5 917
10
1293
4
1176
9 768
10
742
10
1294
6 608
11
237
15
1038
11
330
2
1117
4 82
21
516
16
1222
8
1347
11
922
23
1126
4 868
18
296
24
412
18
407
23
1421
4 930
23
981
2 146
3 348
16
361
18
553
24
1046
2 557
6 396
23
104
3
1197
1 892
6 218
1 865
11
644
6 471
12
938
14
759
2 180
19
739
5
1276
19
1067
7 299
10
715
19
514
7 225
5 574
14
797
18
556
23
531
21
516
15
1463
4
1264
11
1006
12
605
6 592
4 759
6 628
16
1469
8 843
16
638
23
614
24
439
19
775
9 711
8
1304
24
749
16
1143
12
829
16
1063
9 581
8
1113
10
851
14
853
19
781
10 49
16
1035
19
264
18
490
12
922
8 956
23
704
1 507
8 822
3 761
2
1084
14
1221
6 772
7 911
7 545
8 724
10
1005
7
1133
2 189
24
1212
18
1118
21
241
15
1178
3 659
24
867
4 497
8 708
18
1308
11
411
7 500
15
538
8 366
1
1185
19
989
9 759
10
144
8
1210
15
1284
5 361
23
962
5 517
8 637
15
1239
11
992
7 394
2 725
24
114
19
1518
8
1127
12
415
1
1014
15
637
9 860
3 986
21
799
24
211
4
1212
5
1348
8 530
5
1362
9
1114
10
294
8
1414
15
1010
7 987
7 608
10
923
21
837
15
325
16
1127
10
1037
12
980
10
1160
6 331
1 840
18
673
3 561
9 921
24
752
23
1123
21
757
3 722
4 996
19
415
10
650
11
239
5 813
2 349
3 484
16 70
4 596
7 753
9 716
6
1022
4 351
11
185
2 403
12
570
24
905
4 50
9 211
14
737
8 587
5 532
1 544
19 21
5
1096
11
232
1 185
24
507
3 345
16
340
3 755
15
487
7 971
2
1309
11
500
15
968
10
739
8 755
12 60
3 770
18
601
9 284
21 55
15
1144
19
1524
6
1214
7 221
19
391
23
422
9 476
1
1054
1 967
5
1149
1 794
23
1014
4 605
23
860
4 311
14
443
6 659
19 85
1 501
23
346
1 268
8 941
12
918
23
779
18
791
8 561
7
1051
24
554
8 444
14
502
12
1043
9
1080
6 771
1
1274
8 737
19
197
3 703
6 748
10
1029
14
1095
10
1141
9 924
5 962
3 192
10
963
12
521
1
1195
4 529
3
1330
7 900
21
1118
18
1118
5
1060
15
459
18
1107
24
788
16
689
5 392
18
1106
15
848
18
531
12
242
12
669
18
962
14
658
9 799
8 712
21
873
10
1191
11
577
5 499
24
817
23
569
1 529
1
1152
3 813
10
901
21
739
3 641
23
581
10
768
6 810
2
1457
8 698
14
1351
11
1081
21
589
9 848
16
1043
8 710
15
904
11
1171
10 62
16
1097
14
691
3
1183
11
1286
7 606
23 89
7 859
14
481
12
839
2 550
3
1457
3 512
12
465
14
657
21
1149
10
1340
3 765
2 948
9 395
16
662
5 961
23
1389
23
1214
5 890
8 134
22
666
22
777
23
100
22
555
3
1295
11
307
11
605
15
1064
6
1214
1 64
8 907
18
1133
9 636
3
1134
16
1101
6 313
7 791
7
1130
1
1221
21
332
24
697
6 807
2 36
2 747
16
1219
3 859
18
639
18
312
7
1079
10
1074
5 678
18 59
1 325
21 543
In: Computer Science
Problem 1. Checksum Calculation (10 pt.)
Calculate a 16-bit checksum for the following data D1 and D2, respectively.
a. D1 (6 bytes): 01001100 11101001 01101111 01101011 01101111 00000100
b. D2 (6 bytes): 11001100 11101001 01101111 01101011 01101111 00000100
Problem 2. UDP Checksum (10 pt.)
Calculate an UDP checksum based on the following assumption. Note that '0x' indicates a hexadecimal format. Refer to the UDP packet format in the textbook.
Assumption: ▪ Source port = 30001 (=0x7531)
▪ Destination port = 8080 (=0x1F90)
▪ Length = 12 (=0x000C)
▪ Payload = 0x2048 0x8378
In: Computer Science
By using examples differentiate between a single‐alternative and a dual‐ alternative if statement. 8 marks
In: Computer Science
Write a Python program that creates a class which represents a Student Grade. The class includes a function that reads a text file called Course_Score.txt. A sample of the file is provided below. Each row in the file corresponds to the Last Name, First Name, ClassWork score (100), Mid-Term score (100), and Final-Exam score (100). Also include the the following functions to process the content read from the file. a. getData(): This method reads the data from a file and stores the data as a list. b. grade(): This method calculates total score, percentage and grade for each student. A grade (above 90%), B grade (above 80%), C grade (above 60%), D grade (above 50%), and F grade (below 50%) c. highestTotal() and lowestTotal(): These methods calculate highest and lowest total scores in class.
Sample input file: Course_Score.txt
Naji Hasan 90 85 87
Lisa Smith 80 67 70
Andy Malik 75 80 52
Ravi Gupta 90 95 98
Dave Blair 50 61 70
Sara Clark 70 65 81
Sami Moosa 55 50 71
Imed Radhi 90 83 89
Kira Sunny 65 70 69
Hind Ahmed 70 81 88
In: Computer Science
Construct an HTML page that contains a three-level nesting list (you may choose the type of the list you want to use). One item on each level. Use the three attached images as the content of each of the three list items.
-exam prep
-msnlink
-success
example for person who asked in comments
LeBron James Sr. is an American professional basketball player for the Los Angeles Lakers of the National Basketball Association. He is widely considered to be one of the greatest basketball players in NBA history.
Anthony Davis Jr. is an American professional basketball player for the Los Angeles Lakers of the National Basketball Association. He plays the power forward and center positions. Davis was the first overall pick in the 2012 NBA draft.
Jayson Christopher Tatum is an American professional basketball player for the Boston Celtics of the National Basketball Association (NBA).
Gordon Daniel Hayward is an American professional basketball player for the Boston Celtics of the National Basketball Association. He played two seasons of college basketball for Butler University.
In: Computer Science
In: Computer Science
In: Computer Science
1. Given the following values, evaluate the logical value of each expression. [3 each]
int a = 3;
int b = 8;
int c = 3;
int d = 10;
a. b < c && c < d
b. !(a == c && c != d)
c. !(d <= b) || (c > a)
d. a == c || c == d
2. Given the following values, evaluate the logical value of each expression. [4 each]
int g = 6;
int h = 8;
int i = 6;
int j = 15;
a. (i == g || g < i) || j == h
b. h > j || g != i && i != h
Bonus. How would the logical test of a ≤ b ≤ c be written in C++? [1]
In: Computer Science
PYTHON
Write a regular expression that will accept any properly formatted email address, and reject any invalid email address. An example of a valid inputted email address would be "[email protected]".
In: Computer Science
It is a strict requirement that this code is implemented in python. The code must be implemented from scratch not using e.g. numpy etc.
a. Create a function that takes a matrix X of all sizea as input and gives the transposed matrix as output. Example
transposeMatrix([[1,2] ,[3,4] ,[5,6]]) = [[1,3,5] ,[2,4,6]]
b. Create a function that multilpies a matrix X1 and X2 of all sizes. Example
x1 = [[0,1] ,[1,0] ] x2 = [[1, 0] ,[0,-1] ] matrtixMultl(x1,x2) = [[0, -1] ,[1, 0] ]
c. Create a function that takes a square matrix as input and creates its inverse matrix as output. And that is such that matrixMult(A,matrixInvert(A)) is the unit matrix
In: Computer Science
IN C++
Create a class called TextInt. The purpose of a TextInt is to store an integer and convert it to the English text form of the integer when needed, such as ‘zero’ for 0, ‘one’ for 1, and so on, up to ‘nine thousand nine hundred ninety nine’ for 9999. You do NOT need punctuation (commas, hyphens, ‘and’, etc.) The TextInt class should have its own .h and .cpp files. At least the translate function should be implemented in the .cpp file. The rest can be inline functions. A TextInt should have a private member variable number of type int. The TextInt class should have static string members to help translate integers to text. For example, to represent the unique words for translating, you would use:
static string lessThan20[] = {“zero”, “one”, …, “eighteen”, “nineteen”};
static string tens[] = {“twenty”, “thirty”, … static string hundred = “Hundred”;
Any number can be translated using a combination of these, such as 1234 = 1000 + 200 + 30 + 4 = one thousand two hundred thirty four.
The class should have a default constructor with one parameter that initializes the value of the member variable number. Note that the parameter must have a default value to make it a default constructor. It should have public methods (member functions) to return the integer and text versions of number, and to change the value of number.
Overload the following operators so that TextInt can work just like a normal int in code: +, -, /, *, %, and <<.
Note that the first 5 will be almost identical.
For example, this code should work when you are done:
TextInt textInt1(2458), textInt2(1278);
TextInt textInt3 = textInt1 + textInt2;
cout << textInt3 << endl;
Should output: 3736: three thousand seven hundred thirty six
In: Computer Science
In this assignment we are not allowed to make changes to Graph.h it cannot be changed! I need help with the Graph.cpp and the Driver.cpp.
Your assignment is to implement a sparse adjacency matrix data structure Graph that is defined in the header file Graph.h. The Graph class provides two iterators. One iterator produces the neighbors for a given vertex. The second iterator produces each edge of the graph once.
Additionally, you must implement a test program that fully exercises your implementation of the Graph member functions. Place this program in the main() function in a file named Driver.cpp.
The purpose of an iterator is to provide programmers a uniform way to iterate through all items of a data structure using a forloop. For example, using the Graph class, we can iterate thru the neighbors of vertex 4 using:
Graph::NbIterator nit ; for (nit = G.nbBegin(4); nit != G.nbEnd(4) ; nit++) { cout << *nit << " " ; } cout << endl ;
The idea is that nit (for neighbor iterator) starts at the beginning of the data for vertex 4 in nz and is advanced to the next neighbor by the ++ operator. The for loop continues as long as we have not reached the end of the data for vertex 4. We check this by comparing against a special iterator for the end, nbEnd(4). This requires the NbIterator class to implement the ++, !=and * (dereference) operators.
Similarly, the Graph class allows us to iterate through all edges of a graph using a for loop like:
Graph::EgIterator eit ; tuple<int,int,int> edge ; for (eit = G.egBegin() ; eit != G.egEnd() ; eit++) { edge = *eit ; // get current edge cout << "(" << get<0>(edge) << ", " << get<1>(edge) << ", " << get<2>(edge) << ") " ; } cout << endl ;
Note that each edge should be printed only once, even though it is represented twice in the sparse adjacency matrix data structure.
Since a program may use many data structures and each data structure might provide one or more iterators, it is common to make the iterator class for a data structure an inner class. Thus, in the code fragments above, nit and eit are declared asGraph::NbIterator and Graph::EgIterator objects, not just NbIterator and EgIterator objects.
Here are the specifics of the assignment, including a description for what each member function must accomplish.
Requirement: your implementation must dynamically resize the m_nz and m_ci arrays. See the descriptions of Graph(constructor) and addEdge, below.
Requirement: other than the templated tuple class, you must not use any classes from the Standard Template Library or other sources, including vector and list. All of the data structure must be implemented by your own code.
Requirement: your code must compile with the original Graph.h header file. You are not allowed to make any changes to this file. Yes, this prevents you from having useful helper functions. This is a deliberate limitation of this project. You may have to duplicate some code.
Requirement: a program fragment with a for loop that uses your NbIterator must have worst case running time that is proportional to the number of neighbors of the given vertex.
Requirement: a program fragment with a for loop that uses your EgIterator must have worst case running time that is proportional to the number of vertices in the graph plus the number of edges in the graph.
Graph.h:
#ifndef _GRAPH_H_ #define _GRAPH_H_ #include <stdexcept> // for throwing out_of_range exceptions #include <tuple> // for tuple template class Graph { public: // Graph constructor; must give number of vertices Graph(int n); // Graph copy constructor Graph(const Graph& G); // Graph destructor ~Graph(); // Graph assignment operator const Graph& operator= (const Graph& rhs); // return number of vertices int numVert(); // return number of edges int numEdge(); // add edge between u and v with weight x void addEdge(int u, int v, int x); // print out data structure for debugging void dump(); // Edge Iterator inner class class EgIterator { public: // Edge Iterator constructor; indx can be used to // set m_indx for begin and end iterators. EgIterator(Graph *Gptr = nullptr, int indx = 0); // Compare iterators; only makes sense to compare with // end iterator bool operator!= (const EgIterator& rhs); // Move iterator to next printable edge void operator++(int dummy); // post increment // return edge at iterator location std::tuple<int,int,int> operator*(); private: Graph *m_Gptr; // pointer to associated Graph int m_indx; // index of current edge in m_nz int m_row; // corresponding row of m_nz[m_indx] }; // Make an initial edge Iterator EgIterator egBegin(); // Make an end iterator for edge iterator EgIterator egEnd(); // Neighbor Iterator inner class class NbIterator { public: // Constructor for iterator for vertices adjacent to vertex v; // indx can be used to set m_indx for begin and end iterators NbIterator(Graph *Gptr = nullptr, int v = 0, int indx = 0); // Compare iterators; only makes sense to compare with // end iterator bool operator!=(const NbIterator& rhs); // Move iterator to next neighbor void operator++(int dummy); // Return neighbor at current iterator position int operator*(); private: Graph *m_Gptr; // pointer to the associated Graph int m_row; // row (source) for which to find neighbors int m_indx; // current index into m_nz of Graph }; // Make an initial neighbor iterator NbIterator nbBegin(int v); // Make an end neighbor iterator NbIterator nbEnd(int v); private: int *m_nz; // non-zero elements array int *m_re; // row extent array int *m_ci; // column index array int m_cap; // capacity of m_nz and m_ci int m_numVert; // number of vertices int m_numEdge; // number of edges }; #endif
In: Computer Science
Part 1
Recall from Chapter 1 that computer memory is comprised of individual bits of data. A bit (short for binary digit) can store one of only two values, commonly referred to as 0 and 1. However, using two bits, you can represent four different values through the bit patterns 00, 01, 10, and 11. With three bits, you can represent eight different values—via 000, 001, 010, 011, 100, 101, 110, and 111. In general, N bits of memory enable you to represent 2N different values.
Create a Web page named bits.html that contains a text box where the user can enter a number, call it N. At the click of a button, your page should compute and display 2N, the number of values that can be represented using the specified quantity of bits. For example, if the user entered 10 in the text box, the page would display the message:
With 10 bits, you can represent 1024 different values.
Once you have created your page, use it to determine the number of values that each of the following can represent (Test the following numbers to see if they give the correct results)
8 bits (1 byte)
16 bits (2 byte)
32 bits (4 bytes)
64 bits (8 bytes)
Part 2
Most lotteries select winning numbers by drawing numbered balls out of bins. For example, a typical Pick-4 lottery will utilize four bins, each containing balls with numbers starting at 0. If there are 10 balls to choose from in each of four bins, labeled 0 to 9, then 104 = 10,000 different number sequences can potentially be picked. Increasing the number of balls significantly increases the number of possible sequences, which significantly decreases a person’s odds of winning. For example, if there are 20 balls to choose from in each bin, labeled from 0 to 19, then 204 = 160,000 different number sequences could be selected.
Make a copy of the lucky1.html page from Figure 7.4 in your text and name it pick4.html. Then modify this new page so that it simulates a Pick-4 lottery. Your page should have one text box, where the user can enter the highest ball number (it is assumed that the lowest ball number is always 0). When a button is clicked, four random ball numbers should be selected and displayed in a message such as the following:
The Pick-4 winners are: 5-0-8-2
Part 3
Modify your pick4.html page from above so that it makes use of a function in the HEAD. Your function should contain the code previously assigned to the button, and have a name descriptive of the task it performs, such as GeneratePicks or PickNumbers. You should then modify the button’s ONCLICK attribute to call that function.
In: Computer Science
Allow the user to enter the number of people in the party. Calculate and display the amount owed by each person if the bill were to be split evenly among the party members.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mdc.tippcalcula"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
-------------------------------
<?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:columnCount="2" android:useDefaultMargins="true" tools:context=".MainActivity"> <EditText android:id="@+id/amountEditText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:hint="" android:digits="0123456789" android:inputType="number" android:layout_column="0" android:layout_columnSpan="2" android:maxLength="6"/> <TextView android:id="@+id/amountTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_row="0" android:layout_column="0" android:layout_columnSpan="2" android:layout_gravity="fill_horizontal" android:background="@color/amount_background" android:elevation="@dimen/elevation" android:hint="@string/enter_amount" android:padding="@dimen/textview_padding" /> <TextView android:id="@+id/percentTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical|end" android:text="@string/tip_percentage" /> <SeekBar android:id="@+id/percentSeekBar" android:layout_width="wrap_content" android:layout_height="@dimen/seekbar_height" android:layout_gravity="fill_horizontal" android:max="30" android:progress="15"/> <TextView android:id="@+id/TipLabelTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="end" android:text="@string/tip" /> <TextView android:id="@+id/tipTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="fill_horizontal" android:background="@color/result_background" android:elevation="@dimen/elevation" android:gravity="center" android:padding="@dimen/textview_padding" /> <TextView android:id="@+id/totalLabelTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="end" android:text="@string/total" /> <TextView android:id="@+id/totalTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="fill_horizontal" android:background="@color/result_background" android:elevation="@dimen/elevation" android:gravity="center" android:padding="@dimen/textview_padding" /> <EditText android:id="@+id/numberOfPeopleEditText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="fill_horizontal" android:background="@color/result_background" android:elevation="@dimen/elevation" android:gravity="center" android:padding="@dimen/textview_padding" android:ems="10" android:hint="Enter Number Of People" android:digits="012345678" android:inputType="number" android:layout_columnSpan="2"/> <TextView android:id="@+id/eachPersonLabelTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="end" android:text="@string/each" /> <TextView android:id="@+id/eachPersonTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="fill_horizontal" android:background="@color/result_background" android:elevation="@dimen/elevation" android:gravity="center" android:padding="@dimen/textview_padding" /> </GridLayout>
----------
package com.mdc.tippcalcula; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import android.text.Editable; import android.text.TextWatcher; import android.widget.EditText; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.TextView; import java.text.NumberFormat; public class MainActivity extends AppCompatActivity { private static final NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(); private static final NumberFormat percentFormat = NumberFormat.getCurrencyInstance(); private double billAmount = 0.0; private double percent = 0.15; private double numPeople = 0; private TextView percentTextView; private TextView tipTextView; private TextView amountTextView; private TextView totalTextView; private TextView eachPersonTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); amountTextView = (TextView) findViewById (R.id.amountTextView); percentTextView = (TextView) findViewById (R.id.percentTextView); tipTextView = (TextView) findViewById(R.id.tipTextView); totalTextView = (TextView) findViewById(R.id.totalTextView); eachPersonTextView = (TextView) findViewById(R.id.eachPersonTextView); tipTextView.setText(currencyFormat.format(0)); totalTextView.setText(currencyFormat.format(0)); eachPersonTextView.setText(currencyFormat.format(0)); EditText amountEditText = (EditText) findViewById(R.id.amountEditText); amountEditText.addTextChangedListener(amountEditTextWatcher); SeekBar percentSeekBar = (SeekBar) findViewById(R.id.percentSeekBar); percentSeekBar.setOnSeekBarChangeListener(seekBarListener); } private void calculate(){ percentTextView.setText(percentFormat.format(percent)); double tip = billAmount * percent; double total = billAmount + tip; tipTextView.setText(currencyFormat.format(tip)); totalTextView.setText((currencyFormat.format(total))); } private final OnSeekBarChangeListener seekBarListener = new OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean b) { percent = progress / 100.0; calculate(); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }; private final TextWatcher amountEditTextWatcher = new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { try { billAmount = Double.parseDouble(charSequence.toString()) / 100.0; amountTextView.setText(currencyFormat.format(billAmount)); } catch (NumberFormatException e){ amountTextView.setText(""); billAmount = 0.0; } } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void afterTextChanged(Editable editable) { } }; }
------
<?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="textview_padding">12dp</dimen> <dimen name="elevation">4dp</dimen> <dimen name="seekbar_height">40dp</dimen> </resources>
---
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#6200EE</color> <color name="colorPrimaryDark">#3700B3</color> <color name="colorAccent">#03DAC5</color> <color name="amount_background">#BBDEFB</color> <color name="result_background">#ffE0B2</color> </resources>
----
<resources> <string name="app_name">Tip Calculator</string> <string name="enter_amount"> Enter Amount</string> <string name="tip_percentage">15%</string> <string name="tip">Tip</string> <string name="total">Total</string> <string name="number">No. Of People</string> <string name="each">Each Person</string> </resources>
In: Computer Science