import java.util.Scanner;
public class error{
private static class punto{
int x, y;
}
private static class lados{
punto inicio = new punto();
public lados(punto inicio1){
inicio=inicio1;
}
public punto getInicio(){
return inicio;
}
public void setInicio(punto inicio){
this.inicio = inicio;
}
}
public static void main(String[]args){
Scanner leer = new Scanner(System.in);
punto inicio = new punto();
lados arreglo[] = new lados[100];
for(int i=0; i<3; i++){
inicio.x = leer.nextInt();
inicio.y = leer.nextInt();
arreglo[i] = new lados(inicio);
}
for(int i=0; i<3; i++){
System.out.println(arreglo[i].getInicio().x);
System.out.println(arreglo[i].getInicio().y);
}
}
}
what am I doing wrong?
I want to storage ponits(x,y) in an index on an array
but just the last input is storaged in all index...
maybe there is other way to do what I want to do, if someone share it I'd love it.
input:
1
2
3
4
5
6
output:
5
6
5
6
5
6
expected output:
1
2
3
4
5
6
You use the same inicio instance in all lados instances you are creating :
for(int i=0; i<3; i++){
inicio.x = leer.nextInt();
inicio.y = leer.nextInt();
arreglo[i] = new lados(inicio);
}
You should create a new instance of punto for each lados if you want that the information to not be overwritten at each iteration.
Try that :
for(int i=0; i<3; i++){
inicio = new punto()
inicio.x = leer.nextInt();
inicio.y = leer.nextInt();
arreglo[i] = new lados(inicio);
}
By convention, classes should begin by an uppercase letter : Punto, Lados, etc...
Related
I want to have a new random number every time to print it, but it prints the same on. I tried so many thing, but I can't figure out what's wrong. Help me, please!
import 'dart:math';
int next_int() { return new Random().nextInt(100); }
void main()
{
List<int> list = [next_int(), next_int(), next_int()];
// expected new int each time but got the same one
for (var i = 0; i < 3; i++)
{
List<int> cur_list = new List.from(list);
print(cur_list[0]);
}
}
This code will work as you expect:
import 'dart:math';
int next_int() { return new Random().nextInt(100); }
void main()
{
List<int> list = [next_int(), next_int(), next_int()];
// expected new int each time but got the same one
for (var i = 0; i < 3; i++)
{
List<int> cur_list = new List.from(list);
print(cur_list[i]); // <= Use the index value stored in "i" instead of 0
}
}
I am new into using moa and I am having a hard time trying to decode how the clustering algorithms have to be used. The documentation lacks of sample code for common usages, and the implementation is not well explained with comments ... have not found any tutorial either.
So, here is my code:
import com.yahoo.labs.samoa.instances.DenseInstance;
import moa.cluster.Clustering;
import moa.clusterers.denstream.WithDBSCAN;
public class TestingDenstream {
static DenseInstance randomInstance(int size) {
DenseInstance instance = new DenseInstance(size);
for (int idx = 0; idx < size; idx++) {
instance.setValue(idx, Math.random());
}
return instance;
}
public static void main(String[] args) {
WithDBSCAN withDBSCAN = new WithDBSCAN();
withDBSCAN.resetLearningImpl();
for (int i = 0; i < 10; i++) {
DenseInstance d = randomInstance(2);
withDBSCAN.trainOnInstanceImpl(d);
}
Clustering clusteringResult = withDBSCAN.getClusteringResult();
Clustering microClusteringResult = withDBSCAN.getMicroClusteringResult();
System.out.println(clusteringResult);
}
}
And here is the error I get:
Any insights into how the algorithm has to be used will be appreciated. Thanks!
I have updated the code.
It is working as I mentioned in the github, you have to assign header to your instance. See the github discussion
here is the updated code:
static DenseInstance randomInstance(int size) {
// generates the name of the features which is called as InstanceHeader
ArrayList<Attribute> attributes = new ArrayList<Attribute>();
for (int i = 0; i < size; i++) {
attributes.add(new Attribute("feature_" + i));
}
// create instance header with generated feature name
InstancesHeader streamHeader = new InstancesHeader(
new Instances("Mustafa Çelik Instance",attributes, size));
// generates random data
double[] data = new double[2];
Random random = new Random();
for (int i = 0; i < 2; i++) {
data[i] = random.nextDouble();
}
// creates an instance and assigns the data
DenseInstance inst = new DenseInstance(1.0, data);
// assigns the instanceHeader(feature name)
inst.setDataset(streamHeader);
return inst;
}
public static void main(String[] args) {
WithDBSCAN withDBSCAN = new WithDBSCAN();
withDBSCAN.resetLearningImpl();
withDBSCAN.initialDBScan();
for (int i = 0; i < 1500; i++) {
DenseInstance d = randomInstance(5);
withDBSCAN.trainOnInstanceImpl(d);
}
Clustering clusteringResult = withDBSCAN.getClusteringResult();
Clustering microClusteringResult = withDBSCAN.getMicroClusteringResult();
System.out.println(clusteringResult);
}
here is the screenshot of debug process, as you see the clustering result is generated:
image link is broken, you can find it on github github entry link
This is the code I'm trying to use, which seems logical. But doesn't seem to be working.
MyAsFileName.prototype.getTotalScore = function() {
var totalScore = 0;
for (var i = 0; i < allQuestions.length; i++) {
totalScore += allQuestions[i].getCalculatedScore();
if (currentModule.allQuestions[i].parent.questionCorrect == true) {
knowledgePoints++;
} else {
knowledgePoints--;
}
}
debugLog("Total score: " + totalScore);
debugLog(knowledgePoints);
return totalScore;
}
I have allQuestions defined as below:
var allQuestions = Array();
I have knowledgePoints defined as:
this.knowledgePoints = 10;
I have questionCorrect defined as:
this.questionCorrect = false;
Second fresh attempt made with new class as answer below suggested (commented out for now until I figure out how to get working):
// package
// {
/*public class Quiz {
//public
var knowledgePoints: int = 10;
//public
var allQuestions: Array = new Array;
//public
var questionCorrect: Boolean = false;
//public
function getTotalScore(): int {
var totalScore: int = 0;
for (var i = 0; i < allQuestions.length; i++) {
totalScore += allQuestions[i].getCalculatedScore();
if (currentModule.allQuestions[i].parent.questionCorrect) {
knowledgePoints++;
} else {
knowledgePoints--;
}
}
debugLog("Total score: " + totalScore);
debugLog(knowledgePoints);
return totalScore;
}
}*/
//}
This code above outputs two errors in flash console:
Error 1. Attribute used outside of class.
Error 2. 'Int' could not be loaded.
It's a weird (and actually non-AS3 way) way to do this. Instead of creating a unnamed closure which refers weird variables from who-knows where, you should make it a normal AS3 class, something like that (in a file named Quiz.as):
package
{
public class Quiz
{
public var knowledgePoints:int = 10;
public var allQuestions:Array = new Array;
public var questionCorrect:Boolean = false;
public function getTotalScore():int
{
var totalScore:int = 0;
// Your code does not explain how you will that Array.
// It is initially an empty Array of length 0.
for (var i = 0; i < allQuestions.length; i++)
{
totalScore += allQuestions[i].getCalculatedScore();
if (currentModule.allQuestions[i].parent.questionCorrect)
{
knowledgePoints++;
}
else
{
knowledgePoints--;
}
}
// Not sure what it is.
debugLog("Total score: " + totalScore);
debugLog(knowledgePoints);
return totalScore;
}
}
}
Hi guys So i have this problem.. i wanted to print a calendar, utilizing the Calendar API.
condition:
user inputs a Year, anInteger between 1000 - 9999.
then it will output the calendar of that year and 2 more succeeding years.
so if i input 2012 it will output the Calendar of 2012 , 2013 and 2014.
here is the catch it must be SIDEWAYS!
package lessons;
import java.text.DateFormatSymbols;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;
public class lesson3 {
static String[] days = {"SUN","MON","TUE","WED","THU","FRI","SAT"};
static int counter = 0;
public static void main(String[] args) {
GregorianCalendar gcal = new GregorianCalendar();
Scanner scanner = new Scanner(System.in);
System.out.print("Year: ");
int input = scanner.nextInt(); //month input
//System.out.print("YEAR (): ");
//int input2 = scanner.nextInt();
scanner.close();
int year = input;
gcal.set(Calendar.YEAR,input);
for(int monthCount = 0;monthCount<12;monthCount++){
gcal.set(Calendar.MONTH,monthCount); // month counter 0 - 11 producing jan to dec.
monthCheck(monthCount); // converting months to readable names
int x = gcal.get(Calendar.DAY_OF_WEEK);
int max = gcal.getActualMaximum(Calendar.DAY_OF_MONTH);
for(int i = 0;i<days.length;i++){
System.out.printf("%8s",days[i]);
}
System.out.println("");
for(int i = 0; i < x-1;i++){
System.out.printf("%8s","");
counter++;
if(counter%7==0){
System.out.println("");
}
}
for(int i = 1;i<=max;i++){
System.out.printf("%8s",i);
counter++;
if(counter%7==0){
System.out.println("");
}
}
System.out.println("");
//System.out.println(x);
}
}
static void monthCheck(int monthCount){
ArrayList monthsList = new ArrayList<String>();
String[] months = new DateFormatSymbols().getMonths();
for (int i = 0; i < months.length-1; i++) {
String monthArr = months[i];
monthsList.add(months[i]);
}
System.out.println(months[monthCount]);
}
}
This is what i can do with the code Above, click here!
this is what i am aiming for now. CLICK HERE!
I'm looking for digital low pass filter code/library/class for a .net windows forms project, preferably written in c, c++ or c#. I probably need to set the number of poles, coefficients, windowing, that sort of thing. I can't use any of the gpl'd code that's available, and don't know what else is out there. Any suggestions appreciated.
Here is a Butterworth Low Pass filter I wrote for a recent project.
It has some magic numbers as constants that was given to me. If you can figure out how to create the magic numbers with your poles, coefficients, etc, then this might be helpful.
using System;
using System.Collections.Generic;
using System.Text;
namespace Filter
{
public class ButterworthLowPassFilter
{
//filter fc = 2hz, fs = 10hz
private const int LowPassOrder = 4;
private double[] inputValueModifier;
private double[] outputValueModifier;
private double[] inputValue;
private double[] outputValue;
private int valuePosition;
public ButterworthLowPassFilter()
{
inputValueModifier = new double[LowPassOrder];
inputValueModifier[0] = 0.098531160923927;
inputValueModifier[1] = 0.295593482771781;
inputValueModifier[2] = 0.295593482771781;
inputValueModifier[3] = 0.098531160923927;
outputValueModifier = new double[LowPassOrder];
outputValueModifier[0] = 1.0;
outputValueModifier[1] = -0.577240524806303;
outputValueModifier[2] = 0.421787048689562;
outputValueModifier[3] = -0.0562972364918427;
}
public double Filter(double inputValue)
{
if (this.inputValue == null && this.outputValue == null)
{
this.inputValue = new double[LowPassOrder];
this.outputValue = new double[LowPassOrder];
valuePosition = -1;
for (int i=0; i < LowPassOrder; i++)
{
this.inputValue[i] = inputValue;
this.outputValue[i] = inputValue;
}
return inputValue;
}
else if (this.inputValue != null && this.outputValue != null)
{
valuePosition = IncrementLowOrderPosition(valuePosition);
this.inputValue[valuePosition] = inputValue;
this.outputValue[valuePosition] = 0;
int j = valuePosition;
for (int i = 0; i < LowPassOrder; i++)
{
this.outputValue[valuePosition] += inputValueModifier[i] * this.inputValue[j] -
outputValueModifier[i] * this.outputValue[j];
j = DecrementLowOrderPosition(j);
}
return this.outputValue[valuePosition];
}
else
{
throw new Exception("Both inputValue and outputValue should either be null or not null. This should never be thrown.");
}
}
private int DecrementLowOrderPosition(int j)
{
if (--j < 0)
{
j += LowPassOrder;
}
return j;
}
private int IncrementLowOrderPosition(int position)
{
return ((position + 1) % LowPassOrder);
}
}
}
Keith
Ok, I found out how to get the coefficients you used. I downloaded Octave for windows and ran the butter command (as in MatLab) like this:
[b,a] = butter(3, .4, 'low')
Now I can use this code with other fs and fc parameters.