Overview
This assignment will give you experience on the use of classes.
Understand the Application
Every internet user–perhaps better thought of as an Internet connection–has certain data associated with him/her/it. We will oversimplify this by symbolizing such data with only two fields: a name ("Aristotle") and a globally accessible IP address ("139.12.85.191"). We could enhance these by adding other—sometimes optional, sometimes needed—data (such as a MAC address, port, local IP address, gateway, etc), but we keep |
things simple and use only these two for the assignment–any more would not satisfy spec. Also, name is not technically a true datum for this emulation, but we use it to signify the English description of an individual or process that is interacting with the Internet. |
The Program Spec
Class InternetUser Spec
Your class is named InternetUser. Static Class Constants
Define intended constant values
MAX_NAME_LEN = [you choose an int] MIN_NAME_LEN = [you choose an int] MAX_IP4_LEN = 15 # xxx.xxx.xxx.xxx MIN_IP4_LEN = 7 # x.x.x.x
DEFAULT_NAME = "[you choose a str]" DEFAULT_IP = "[you choose a str]"
"Private" Member Data Instance Members
▪ name - a string that represents the description of the internet user entity.
▪ ip - a string thar represents the IP4 address: 1.2.3.4 or 200.200.200.200. We will accept any string that
can hold addresses from 0.0.0.0 to 999.999.999.999, even though the ints between the dots are out-of- range for real addresses.
Client Callable Methods
▪ __init__(self, name, ip) - a constructor. It should take default arguments as defined by the indented constants.
▪ __str___(self) - a method that provides a nicely formatted return string for potential screen I/O. Here's a possible string that received my choice of defaults:
Name: (undefined) IP Aaddr: 0.0.0.0
private Methods
• private static validation helpers to filter client parameters. These will support your public methods. You should only test lengths here, since we don't really need to check the number of periods or size of each of the four ints in an IP address. We will let that slide for this assignment. (If you do not know what an IP address looks like, now's a good time to Google it. We're using IP4, not IP6, format.)
Client Test of Class InternetUser
Here is a sample run from my test (but you will have to imagine/deduce the source I used and create your own source for your testing, which will produce a different output).
Where it All Goes
There are now a variety of program elements, so let's review the
order in which things appear in your .py file:
class definition(s)
global-scope function definition(s) [You may not need them for this assignment.]
main program
In other words, internet_user.py will look like this:
# ---------------- SOURCE ---------------------------------------- class InternetUser:
# intended class constants ------------------------------------
MAX_NAME_LEN = 50
...
# constructor method ------------------------------------ def __init__((self, name, ip):
...
# mutator ("set") methods ------------------------------- ...
# accessor ("get") methods ------------------------------- ...
# helper methods for entire class ----------------- ...
# ------------- CLIENT --------------------------------------------------
# Create 4 InternetUser objects internet_user_num_1 =
InternetUser() ...
The main program:
Instantiate two or more InternetUser objects, some using the default arguments and some passing parameters.
Immediately display all objects.
Mutate one or more of the members and, after that, use the stringizer to assist a screen output so we can
see what your objects contain.
Next, test one or more accessors.
Finally, call two or more mutators and constructors, providing both legal and illegal arguments and
checking the return values (thus demonstrating that the mutators do the right thing).
Display all objects a second time.
More:
Be sure that all output is descriptive. Use labels to differentiate your output sections and full sentences
in your mutator/accessor tests.
No user input should be done in this program.
Sample run:
Example Run of InternetUser Class Testing
""" ------------------------- RUN ----------------------------
InternetUser Class Testing *********************** Name:
(undefined)
IP Aaddr: 0.0.0.0
Name: dns
IP Aaddr: 75.75.75.75
Name: bad
IP Aaddr: 0.0.0.0
--------------------------------------------------------------- """
Example Run of InternetUser Class Testing
Base Class Testing Name: (undefined) IP Aaddr: 0.0.0.0 Name: dns IP Aaddr: 75.75.75.75 Name: bad IP Aaddr: 0.0.0.0 success as expected
RSA Encryption Keys and Class Communicator
To encrypt a message, each InternetUser has to also be a Communicator, and to do that, the object has to have more than just a name and an ip; it has to have two encryption keys, a public key and a private key. So the most fundamental aspect of this class are those keys. Each key is a pair of integers, or in our current regime, each key is an IntPairmember object:
IntPair publicKey; IntPair privateKey;
Two examples of such pairs are
publicKey: (23, 703) privateKey: (214, 703)
and
publicKey: (29, 957779) privateKey: (726591, 957779)
Notice two things about the pair:
The second number in the pair is the same, and
the first number in the pair is a prime number.
While the sameness of the second number is normally considered a bad duplication of data, we'll allow it in RSA emulation since it is part of the formal definition of the two keys and we want to parallel the math as closely as possible.
Four constructors signatures:
Communicator(); Communicator( long firstPrime, long secondPrime ); Communicator( String name, String ip ); Communicator( String name, String ip, long firstPrime, long secondPrime );
Accessors - one for each key. Only the encryption keys (which are IntPair type) have accessors. Remember, accessor return type always agrees with that of the member.
toString() - a method that returns a String of the entire object's private data in a readable form. Make proper use of method chaining.
Private Methods
bool computeBothEncrKeys() - This is the method that uses the primes to build encryption keys. It takes the following action (this is how RSA encryption key generation is accomplished):
Generate n = p * q;
Generate phi = φ(n) which is the number of primes smaller than n. φ(n) is called "Euler's totient function," and in the special case where n is the product of two primes (but not generally), φ(n) is simply (p - 1) * (q - 1).
Example Test of Communicator Class (done in same run as overall program run)
Example of Derived Class Constructor Testing Name: (undefined) IP Aaddr: 0.0.0.0 (p, q) n, phi, e, d: (0, 0) 0, 0, 0, 0 public key(0, 0) private key(0, 0) Name: Raja IP Aaddr: 10.1.10.1 (p, q) n, phi, e, d: (0, 0) 0, 0, 0, 0 public key(0, 0) private key(0, 0) Name: kumar IP Aaddr: 127.90.32.14 (p, q) n, phi, e, d: (0, 0) 0, 0, 0, 0 public key(0, 0) private key(0, 0) Name: sandy IP Aaddr: 0.0.0.0 (p, q) n, phi, e, d: (19, 37) 703, 648, 463, 372 public key(463, 703) private key(372, 703) Name: rosi IP Aaddr: 1.1.1.1 (p, q) n, phi, e, d: (0, 0) 0, 0, 0, 0 public key(0, 0) private key(0, 0) Derived and Base Class Mutator Testing Name: karthick IP Aaddr: 0.0.0.0 (p, q) n, phi, e, d: (809, 821) 664189, 662560, 463, 642671 public key(463, 664189) private key(642671, 664189) PROGRAM:
public class Foothill
{
public static void main(String[] args)
{
InternetUser user,user1,user2,user3;
System.out.println("/*---------------------------------------------------------");
System.out.println("Base Class Testing \n");
user = new InternetUser();
user1 = new InternetUser("dns","");
user2 = new InternetUser("n","75.75.75.75");
user3 = new InternetUser();
user3.setName("n");
System.out.println(user);
System.out.println(user1);
System.out.println(user2);
System.out.println(user3);
System.out.println("\nsuccess as expected");
System.out.println("------")
System.out.println("Derived Class Constructor Testing
\n");
System.out.println("----------------");
Communicator user,user1,
user2, user3, user4, user5;
user = new
Communicator();
System.out.println(user);
System.out.println("\n\n----------------");
user1 = new
Communicator("salim lakhani","10.1.10.1");
System.out.println(user1);
System.out.println("\n\n----------------");
user2 = new
Communicator("yan kam","127.90.32.14");
System.out.println(user2);
System.out.println("\n\n----------------");
user3 = new
Communicator("sally","", 19, 37);
System.out.println(user3);
System.out.println("\n\n----------------");
user4 = new
Communicator("betty","1.1.1.1");
System.out.println(user4);
System.out.println("\n\n----------------");
user5 = new
Communicator("giral","", 809, 821);
System.out.println(user5);
System.out.println("---------------------------------------------------------
*/");
}
}
public class Communicator extends InternetUser {
public final static int ERROR_FLAG_NUM =
0;
private final static long MAX_PQ = (long)
Math.sqrt(Long.MAX_VALUE);
private IntPair publicKey;
private IntPair privateKey;
private long firstPrime;
private long secondPrime;
private long n, phi, e, d;
public Communicator() {
super();
setPrimes(0, 0);
}
public Communicator(long firstPrime, long secondPrime)
{
super();
setPrimes(firstPrime,
secondPrime);
}
public Communicator(String name, String ip)
{
super(name, ip);
setPrimes(0, 0);
}
public Communicator(String name, String ip,
long firstPrime, long secondPrime) {
super(name, ip);
setPrimes(firstPrime,
secondPrime);
}
public boolean setPrimes(long first, long second)
{
boolean isValid =
false;
if (first != second
&& EncryptionSupport.isPrime(firstPrime) &&
EncryptionSupport.isPrime(secondPrime)
& firstPrime <= MAX_PQ && secondPrime <=
MAX_PQ)
{
firstPrime = first;
secondPrime = second;
if (computeBothEncrKeys()) {
isValid = true;
}
}
if (isValid)
{
return true;
}
set all numeric to ERROR_FLAG_NUM
n =
ERROR_FLAG_NUM;
phi =
ERROR_FLAG_NUM;
e =
ERROR_FLAG_NUM;
d =
ERROR_FLAG_NUM;
publicKey = new
IntPair(ERROR_FLAG_NUM, ERROR_FLAG_NUM);
privateKey = new
IntPair(ERROR_FLAG_NUM, ERROR_FLAG_NUM);
return false;
}
public String toString()
{
StringBuilder result =
new StringBuilder();
result.append(super.toString());
result.append(
"\n(p, q) n, phi, e, d: (" + firstPrime + ", " + secondPrime + ")"
+ " " + n + ", " + phi + ", " + e + ", " + d);
result.append("\npublic
key" + publicKey);
result.append("\nprivate
key" + privateKey);
return
result.toString();
}
public IntPair getPublicKey()
{
return publicKey;
}
public IntPair getPrivateKey()
{
return privateKey;
}
private boolean computeBothEncrKeys()
{
if (firstPrime !=
ERROR_FLAG_NUM && secondPrime != ERROR_FLAG_NUM)
{
n = firstPrime * secondPrime;
phi = (firstPrime - 1) * (secondPrime - 1);
e = EncryptionSupport.getSmallRandomPrime();
d = EncryptionSupport.inverseModN(e, n);
publicKey = new IntPair(e, n);
privateKey = new IntPair(d, n);
return true;
}
return false;
}
}
import java.util.Random;
class EncryptionSupport
{
static private Random randObject = new
Random(System.currentTimeMillis());
public static boolean isPrime(long x)
{
long k, loopLim;
if (x < 2)
return false;
if (x < 4)
return true;
if (x % 2 == 0 || x % 3
== 0)
return false;
// now use the fact
the all primes of form 6k +/- 1
loopLim =
(long)Math.sqrt(x);
for (k = 5; k <=
loopLim; k += 6)
{
if (x % k == 0 || x % (k + 2) == 0)
return false;
}
return true;
}
public static long inverseModN(long a, long
n)
{
// uses extended
euclidean algorithm giving as + nt = gcd(n, a),
// with gcd(n, a) = 1,
and s, t discovered. s = 1/a, and t ignored
long s, t, r, sPrev, tPrev, rPrev, temp, q, inverse;
// special key
encryption conditions; we will pick some prime e >= 3 for
a
if (a < 3 || a >=
n || !isPrime(a))
return 0; // error
// we are now guaranteed 3 <= a < n and gcd(a, n) = 1;
// initialize working
variables
s =
0; t =
1; r = n;
sPrev =
1; tPrev = 0; rPrev = a;
while (r != 0)
{
q = rPrev / r;
temp = r;
r = rPrev - q * r;
rPrev = temp;
temp = s;
s = sPrev - q * s;
sPrev = temp;
temp = t;
t = tPrev - q * t;
tPrev = temp;
}
inverse = sPrev %
n;
if (inverse <
0)
inverse += n;
return inverse;
}
public static long
getSmallRandomPrime()
{
int index;
long lowPrimes[] =
{
19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67,
71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113,
127, 131, 137, 139, 149, 151, 157, 163, 167, 173,
179, 181, 191, 193, 197, 199, 211, 223, 227, 229,
233, 239, 241, 251, 257, 263, 269, 271, 277, 281,
283, 293, 307, 311, 313, 317, 331, 337, 347, 349,
353, 359, 367, 373, 379, 383, 389, 397, 401, 409,
419, 421, 431, 433, 439, 443, 449, 457, 461, 463,
467, 479, 487, 491, 499, 503, 509, 521, 523, 541
};
long arraySize =
lowPrimes.length;
// pick prime in the
above array bet 0 and arraySize - 1
index = (int)(
randObject.nextDouble() * arraySize );
return
lowPrimes[index];
}
public static long
getMedSizedRandomPrime()
{
int index;
long medPrimes[] =
{
541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607,
613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677,
683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761,
769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853,
857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937,
941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013,
1019,
1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087,
1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153,
1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223,
};
long arraySize =
medPrimes.length;
// pick prime in the
above array bet 0 and arraySize - 1
index =
(int)(randObject.nextDouble() * arraySize );
return
medPrimes[index];
}
}
public class InternetUser
{
public final static int MIN_NAME_LENGTH =
2;
public final static int MAX_NAME_LENGTH =
50;
public final static int MIN_IP_LENGTH = 7;
public final static int MAX_IP_LENGTH =
15;
public final static String DEFAULT_IP =
"0.0.0.0";
public final static String DEFAULT_NAME =
"(undefined)";
private String name;
private String ip;
InternetUser(String userName, String
ipAddress)
{
if
(validateName(userName))
{
this.name = userName;
} else
{
this.name = "bad";
}
if
(validateIp(ipAddress))
{
this.ip = ipAddress;
} else
{
this.ip = DEFAULT_IP;
}
}
InternetUser()
{
this.name =
DEFAULT_NAME;
this.ip =
DEFAULT_IP;
}
public String getName()
{
return name;
}
public String getIp()
{
return ip;
}
public void setName(String name)
{
if
(validateName(name))
{
this.name = name;
} else
{
this.name = "bad";
}
}
public void setIp(String ip)
{
if
(validateIp(ip))
{
this.ip = ip;
} else
{
this.ip = DEFAULT_IP;
}
this.ip = ip;
}
public String toString()
{
String output;
output = "Name: " + name
+ "\n" + "IP Aaddr: " + ip + "\n";
return output;
}
private static boolean validateName(String
n)
{
if (n.length() >
MAX_NAME_LENGTH || n.length() < MIN_NAME_LENGTH)
{
return false;
}
return true;
}
private static boolean validateIp(String
ipA)
{
if (ipA.length() >
MAX_IP_LENGTH || ipA.length() < MIN_IP_LENGTH)
{
return false;
}
return true;
}
}
class IntPair
{
public long firstInt;
public long secondInt;
IntPair()
{
firstInt = secondInt =
0;
}
IntPair(long frst, long scnd)
{
firstInt = frst;
secondInt = scnd;
}
public String toString()
{
return "(" + firstInt +
", " + secondInt + ")";
}
};
Overview This assignment will give you experience on the use of classes. Understand the Application Every...
IN PYTHON Assignment Overview This assignment will give you experience on the use of classes. Understand the Application The assignment is to first create a class calledTripleString.TripleStringwill consist of threeinstance attribute strings as its basic data. It will also contain a few instance methods to support that data. Once defined, we will use it to instantiate TripleString objects that can be used in our main program. TripleString will contain three member strings as its main data: string1, string2, and string3....
JAVA :The following are descriptions of classes that you will create. Think of these as service providers. They provide a service to who ever want to use them. You will also write a TestClass with a main() method that fully exercises the classes that you create. To exercise a class, make some instances of the class, and call the methods of the class using these objects. Paste in the output from the test program that demonstrates your classes’ functionality. Testing...
Assignment Overview This assignment will give you more experience on the use of strings and iterations. The goal of this project is to use Google’s currency converter API to convert currencies in Python and display the result to user by processing the returned results. Assignment Background The acronym API stands for “Application Programming Interface”. It is usually a series of functions, methods or classes that supports the interaction of the programmer (the application developer, i.e., you) with some particular program....
This java assignment will give you practice with classes, methods, and arrays. Part 1: Player Class Write a class named Player that stores a player’s name and the player’s high score. A player is described by: player’s name player’s high score In your class, include: instance data variables two constructors getters and setters include appropriate value checks when applicable a toString method Part 2: PlayersList Class Write a class that manages a list...
This assignment shpuld be code in java. Thanks Overview In this assignment you will write a program that will model a pet store. The program will have a Pet class to model individual pets and the Assignment5 class will contain the main and act as the pet store. Users will be able to view the pets, make them age a year at a time, add a new pet, and adopt any of the pets. Note: From this point on, your...
write in java and please code the four classes with the requirements instructed You will be writing a multiclass user management system using the java. Create a program that implements a minimum of four classes. The classes must include: 1. Employee Class with the attributes of Employee ID, First Name, Middle Initial, Last Name, Date of Employment. 2. Employee Type Class that has two instances of EmployeeType objects: salaried and hourly. Each object will have methods that calculates employees payrol...
Assignment Overview In Part 1 of this assignment, you will write a main program and several classes to create and print a small database of baseball player data. The assignment has been split into two parts to encourage you to code your program in an incremental fashion, a technique that will be increasingly important as the semester goes on. Purpose This assignment reviews object-oriented programming concepts such as classes, methods, constructors, accessor methods, and access modifiers. It makes use of...
Assignment 3: Ultimate Frisbee For this assignment, you will create a hierarchy of five classes to describe various elements of a an ultimate frisbee (Links to an external site.)Links to an external site. team. Ultimate frisbee is a non-contact sport with players at a position of “cutter” or “handler”. A team usually also has a head coach and possibly one or more assistant coaches. An ultimate team has seven players on the field, with four players at the position of...
The goal of this assignment is to give you some experience building a program that uses objects. You must work in teams of 2 to complete this assignment. You will use the BankAccount class you created in Lab 3 to create a banking program. The program must display a menu to the user to allow the user to select a transaction like deposit, withdraw, transfer, create a bank account, and quit the program. The program should allow a user to...
In Java plz due today Assignment 4 - Email, Shwitter and Inheritance Select one option from below. All (both) options are worth the same number of points. The more advanced option(s) are provided for students who find the basic one too easy and want more of a challenge. OPTION A (Basic): Message, EMail and Tweet Understand the Classes and Problem Every message contains some content ("The British are coming! The British are coming!"). We could enhance this by adding other...