java generics: defining objects [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to learn Java and having trouble understanding generics. I'm attempting to define an object of Integer data type, then use the object to to call a generic method.
Guided by the API and some web resources, I've been trying all sorts of things but I don't know if i'm on the right track in terms of simply defining the object.
SearchSortAlgorithms.java:
public class SearchSortAlgorithms<T> implements SearchSortADT<T>
{
public void quickSort(T[] list, int length)
{
recQuickSort(list, 0, length - 1);
}
}
TestQuickSort.java
public class TestQuickSort
{
static void main(String [] args)
{
// define an Integer array of 50000 elements
Integer[] anArray = new Integer[5000];
// load the array with random numbers using
// a for loop and Math.random() method - (int)(Math.random()*50000)
for (int i = 0; i < anArray.length; i++) {
anArray[i] = (int)(Math.random() * i);
}
// define an object of SearchSortAlgorithm with Integer data type
// use this object to call the quickSort method with parameters: your array name and size-50000
Integer aSortedArray = new Integer(5000);
public void quickSort(anArray, 5000) {
TestQuickSort<Integer> aSortedArray = new TestQuickSort<Integer>();
return aSortedArray.quickSort(anArray, 5000);
}
// print out the first 50 array elements with a for loop
// they have to be sorted now
for (int k = 0; k <= 50; k++) {
System.out.print(aSortedArray[k] + " ");
}
}
}
Errors on these lines:
public int TestQuickSort () {
TestQuickSort<Integer> aSortedArray = new TestQuickSort<Integer>();
aSortedArray = quickSort(anArray, 5000);
}
-Illegal start of expression: I wonder if my attempt at creating the constructor is right
-; expected

Ignoring any other potential errors in your code (or due to its presentation to us here), you are attempting to declare another method in main.
public int SearchSortAlgorithm () {
TestQuickSort<Integer> aSortedArray = new TestQuickSort<Integer>();
aSortedArray = quickSort(anArray, 5000);
}
This needs to be moved out of main. And also fixed to actually return an int. And main's signature should be public static void main(String[] args).
Although, it should really be returning an int[] instead...
public static int[] searchSortAlgorithm (final int[] anArray) {
TestQuickSort<Integer> aSortedArray = new TestQuickSort<Integer>();
return quickSort(anArray, 5000);
}
...and called in your main method like this...
int[] aSortedArray = searchSortAlgorithm(anArray);
for (int k = 0; k <= 50; k++) { // would be better to use aSortedArray.length
System.out.print(aSortedArray[k] + " ");
}

Related

java extended class inheritance [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Sum s = new Sum();
Sum.SetToZero z = new Sum.SetToZero();
Scanner input = new Scanner(System.in);
String read = input.nextLine();
while (!read.equals("end")) {
if (read.equals("add")) {
s.add()
}
else if (read.equals("get")) {
System.out.println(s.returnTotal());
}
else if (read.equals("zero")) {
z.zero();
}
read = input.nextLine();
}
class:
public class Sum {
int total = 0;
public void add() {
total += 1;
}
public int returnTotal() {
return total;
}
public static class SetToZero extends Sum {
public void zero() {
total = 0;
}
}
}
input:
add
add
zero
add
get
add
get
end
output:
3
4
output wanted:
1
2
Shouldn't the subclass inherit the total and set it to zero? What am I doing wrong? I know I could just move the zero into the main class but I want it to be in a separate class. thx for your help.
By making your total variable static, you can get the desired output.
class Sum {
static int total = 0;
public void add() {
total += 1;
}
public int returnTotal() {
return total;
}
public static class SetToZero extends Sum {
public void zero() {
total = 0;
}
}
}
Apart from things pointed out in names like not using lowecase letters to start your class name; I think the reason why it's not working is because you're using two different variable instances for Sum and Sum.SetToZero. You don't need to create a new variables since SetToZero has all the attributes of Sum. I think you should change this:
Sum s = new Sum();
Sum.SetToZero z = new Sum.SetToZero();
Sum.SetToZero s = new Sum.SetToZero(); // use s for all operations
Here is how your modified main method would look:
public static void main(String[] args) {
Sum.SetToZero s = new Sum.SetToZero();
Scanner input = new Scanner(System.in);
String read = input.nextLine();
while (!read.equals("end")) {
if (read.equals("add")) {
s.add();
}
else if (read.equals("get")) {
System.out.println(s.get());
}
else if (read.equals("zero")) {
s.zero();
}
read = input.nextLine();
}
}
When I ran this, I saw expected output:
src : $ java Sum
add
add
zero
add
get
1
add
get
2
end

Create an array checking previous items in another array [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to create an array of numbers, starting from a big number, which is a string, and then splited to create an array.
This array has numbers from 1 to 6.
I want to create a new array of 29 items, checking the last 3 items. For instance, if the current item to add is number 4, it will check for item 3, 2, and 1. I want it to check if any of the other 3 items are the same as the current, and if is the same, it will skip to next number.
The idea, is not to have repeated numbers in a "range" of 4 items.
I also want to do another thing, and is that if the current item is number 1, it would check the last 6 items, instead of just the last 3. Only when the item is number 1. I just haven't found a way to do it.
I hope someone could help me.
Here's the current code:
import java.io.*;
import java.util.*;
public class Randomizer {
public static String numbers = "1632544362511653244213652164535316243164251654235231465312645164233462153465124361522316545326412354165412633214654531626513246124351423565431623162453625412564325134634562136145256142342365161423542563151426325146335241613645225431656324164351215263453642123416561345264215321536436451243156263542125631415624314536235124662145152436352164436512645312264315432651216345624315421563516423465213614352621543342516352614";
public static void main(String[] args) {
show("\n");
String[] nums = numbers.split("(?!^)"), finalNums = new String[29];
for (int i = 0; i < finalNums.length; i++) {
finalNums[i] = newnumber(nums, i);
}
for (String num : finalNums) {
show(num + "-");
}
}
public static String newnumber(String[] array, int start) {
String num = "x";
String act = array[start];
for (int i = start; i < array.length; i++) {
int a = start - 1, b = start - 2, c = start - 3, d = start - 4;
if (a >= 0 && b >= 0 && c >= 0 && d >= 0) {
String na = array[a], nb = array[b], nc = array[c], nd = array[d];
if (act != na && act != nb && act != nc && act != nd) {
num = act;
} else {
}
} else {
num = act;
}
}
return num;
}
public static void show(String s) {
System.out.print(s);
}
public static void show(int s) {
System.out.print(s);
}
public static void show(float s) {
System.out.print(s);
}
public static void show(double s) {
System.out.print(s);
}
}
The output is:
1-6-3-2-5-4-4-3-6-2-5-1-1-6-5-3-2-4-4-2-1-3-6-5-2-1-6-4-5
What I expect to get is something like:
1-6-3-2-5-4-6-1-2-5-3-4-6-5-1-2-5-4-6-3-2-1-2-4-5-6-3-2-1

appending an array of string to arraylist of type Integer in Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an ArrayList<Integer> called nodes. I want to assign elements from array[] to elements in ArrayList. Such that 1st element in arraylist will have first element in array as its property.. and so on. However, there are only 6 elements in array and thus for 7th element its again first element from array[]. Array is of type Integer.
public static String[] Interest = new String[] {"I1","I2","I3","I4","I5","I6"};
public static void main(String[] args){
System.out.println("Enter number of nodes");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i=0;i<=n;i++) {
nodes.add(i);
}
System.out.println(nodes);
}
I want to set up an interest profile for each element in node. For that I have an array called Interest which has 6 elements in it. Now say for the first element in node, I want to set up preferences for Interests. For example, the first element could have interest profile as I1 = 10, I2 = 8, ..., I6 = 2 and so on.
I am thinking of using a method like SetIntensity(ArrayList element, array of integers) which will set interest profile for an element passed as a parameter. I am not sure if it's correct but I am expecting something like this:
public static void setInterest(List<String> array){
String[] Interest = new String[]{"I1","I2","I3","I4","I5","I6"};
for(int k=0;k<array.size();k++){
array.get(k);
for(int j=0;j<Interest.length;j++){
}
}
}
Sounds like you need a Node class with a Map to store preferences.
class Node {
Integer id;
Map<String, Integer> interestPreferences;
public Node(Integer id) {
this.id = id;
this.interestPreferences = new HashMap<String, Integer>();
}
void setPreference(String key, Integer value) {
interestPreferences.put(key, value);
}
Interest getPreference(String key) {
return interestPreferences.get(key);
}
}
Then use it like this:
public static List<Node> nodes = new ArrayList<Node>();
public static String[] Interest = new String[] {"I1","I2","I3","I4","I5","I6"};
public static void main(String[] args){
System.out.println("Enter number of nodes");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i=0;i<=n;i++) {
Node node = new Node(i);
node.setPreference("I1", 10);
node.setPreference("I2", 8);
//....
node.setPreference("I6", 2);
nodes.add(node);
}
System.out.println(nodes);
}

How do I invoke a method with array parameter in java? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an assignment in which I have to perform operations on array in Java, I have to make separate functions of each operation, which I will write but I can not figure out how to invoke a method with array parametres. I usually program in c++ but this assignment is in java. If any of you can help me, I'd be really thankful. :)
public class HelloJava {
static void inpoot() {
Scanner input = new Scanner(System.in);
int[] numbers = new int[10];
System.out.println("Please enter 10 numbers ");
for (int i = 0; i < numbers.length; i++) {
numbers[i] = input.nextInt();
}
}
static void outpoot(int[] numbers) {
for(int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
}
public static void main(String[] args) {
inpoot();
outpoot(numbers); //can not find the symbol
}
}
Your inpoot method has to return the int[] array, and then you pass it to outpoot as a parameter:
public class HelloJava {
static int[] inpoot() { // this method has to return int[]
Scanner input = new Scanner(System.in);
int[] numbers = new int[10];
System.out.println("Please enter 10 numbers ");
for (int i = 0; i < numbers.length; i++) {
numbers[i] = input.nextInt();
}
return numbers; // return array here
}
static void outpoot(int[] numbers) {
for(int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
}
public static void main(String[] args) {
int[] numbers = inpoot(); // get the returned array
outpoot(numbers); // and pass it to outpoot
}
}
When you call outpoot it should be
outpoot (numbers);

Performing a method on an int [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to generate a random number for an int through a method. I know that this cannot be done because an int is a primitive type and I get an int cannot be deferenced error. Is there is away around it to where I can still use the method to get the value for the int?
public int move()
{
Random random = new Random();
int generatedNum = random.nextInt(7 - 1) + 1;
return generatedNum;
}
public static void main(String[] args)
{
int player1 = 0;
int player2 = 0;
player1 = player1.move();
player2 = player2.move();
}
What are you trying to do?
This would work, but no idea if it is what you want...
// made this static so you don't need a class reference
public static int move()
{
Random random = new Random();
int generatedNum = random.nextInt(7 - 1) + 1;
return generatedNum;
}
public static void main(String[] args)
{
int player1 = 0;
int player2 = 0;
// Now we just call the static method move to set the ints.
player1 = move();
player2 = move();
}
int is a primitive type and Integer is final, so no, you will never be able to write 5.move().
You could write a class that just wraps an integer, but will not be able to do arithmetics with it.
Maybe Iterator<Integer> is an interface worth implementing.
You can create your own class similar to the Integer class (which is unfortunately final or you'd be able to subclass that). Then add whatever methods you'd like to that class. A little inconvenient to use when you want to retrieve the value, but not too messy.

Categories

Resources