Adding and subtract complex numbers using OOP structure
I have here a code that should print the sum and difference of two complex
numbers. The instructions given are:
make the methods add, subtract, and print to be void and
test using the constructor's object.
public class Complex {
/**
* @param args
*/
public double real;
public double imag;
public String output = "";
public Complex(double real, double imag){
this.real += real;
this.imag += imag;
}
public Complex(){
real = 0;
imag = 0;
}
public double getReal(){
return real;
}
public void setReal(double real){
this.real = real;
}
public double getImag(){
return imag;
}
public void setImag(double imag){
this.imag = imag;
}
public void add(Complex num){
this.real = real + num.real;
this.imag = imag + num.imag;
}
public void subtract(Complex num){
this.real = real - num.real;
this.imag = imag - num.imag;
}
public void print(){
//
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Complex c1 = new Complex(4.0, 8.5);
Complex c2 = new Complex(8.0, 4.5);
c1.add(c2);
c1.subtract(c2);
c1.print(); //expected answer 12.0 + 13.0i
//-4.0 - 4.0i
}
}
The expected answers are 12.0 + 13.0i and -4.0 - 4.0i. Please help me with
the method print. Thank you.
Saturday, 31 August 2013
Do pixel densities affect positioning of elements across various devices?
Do pixel densities affect positioning of elements across various devices?
I'm building a custom slideshow and I want my slide controls to always be
at a certain position on the bottom of the window. To accomplish this I'm
using jQuery and setting the top property of the div containing the
controls to the window height - 50px.
My question is, if the site is viewed on a device with a high pixel
density does this approach still work? I have nothing to test on and
appreciate any feedback. In my mind I think that if a device has a high
pixel density 50px will only move it slightly, but I'm hoping I am wrong.
I'm building a custom slideshow and I want my slide controls to always be
at a certain position on the bottom of the window. To accomplish this I'm
using jQuery and setting the top property of the div containing the
controls to the window height - 50px.
My question is, if the site is viewed on a device with a high pixel
density does this approach still work? I have nothing to test on and
appreciate any feedback. In my mind I think that if a device has a high
pixel density 50px will only move it slightly, but I'm hoping I am wrong.
How to add a button to iOS text selection action control
How to add a button to iOS text selection action control
Does anybody know how I can add a button like the "Share" button in the
example to that kind of control. I have a UIWebView where I want to offer
the user with special actions to take after she selects text in it.
Example image here
Does anybody know how I can add a button like the "Share" button in the
example to that kind of control. I have a UIWebView where I want to offer
the user with special actions to take after she selects text in it.
Example image here
jQuery search in array of object
jQuery search in array of object
Lets say I have this html :
<ul>
<li class="cls">one</li>
<li class="cls active">tow</li>
<li class="cls">here</li>
<li class="cls">after</li>
</ul>
I'm selecting all .cls by this jquery selector : $('.cls') and put them in
a variable:
var c=$('.cls');
c is an array of object now. I want to select .active item in this array
by jquery methods. I do know i can use $('.cls.active') but this is not
what I'm lookin for. I want to use c. Is there any solution ?
note: c.find('.active') not working, because .find() searching in childs.
Lets say I have this html :
<ul>
<li class="cls">one</li>
<li class="cls active">tow</li>
<li class="cls">here</li>
<li class="cls">after</li>
</ul>
I'm selecting all .cls by this jquery selector : $('.cls') and put them in
a variable:
var c=$('.cls');
c is an array of object now. I want to select .active item in this array
by jquery methods. I do know i can use $('.cls.active') but this is not
what I'm lookin for. I want to use c. Is there any solution ?
note: c.find('.active') not working, because .find() searching in childs.
Friday, 30 August 2013
onchange event on input type=range is not triggering in firefox while dragging
onchange event on input type=range is not triggering in firefox while
dragging
When i played with <input type="range">, Firefox triggers an onchange
event only if we drop the slider to a new position where Chrome and others
triggers onchange events while the slider is dragged.
How can i make it happen on dragging in firefox?
HTML
<span id="valBox"></span>
<input type="range" min="5" max="10" step="1" onchange="showVal(this.value)">
SCRIPT
function showVal(newVal){
document.getElementById("valBox").innerHTML=newVal;
}
dragging
When i played with <input type="range">, Firefox triggers an onchange
event only if we drop the slider to a new position where Chrome and others
triggers onchange events while the slider is dragged.
How can i make it happen on dragging in firefox?
HTML
<span id="valBox"></span>
<input type="range" min="5" max="10" step="1" onchange="showVal(this.value)">
SCRIPT
function showVal(newVal){
document.getElementById("valBox").innerHTML=newVal;
}
Thursday, 29 August 2013
IOS heightForRowAtIndexPath, setting height for empty cells
IOS heightForRowAtIndexPath, setting height for empty cells
i am setting the heightForRowAtIndexPath like this:
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
/* Contains the string */
NSString *allStrings = [self convertMainData:indexPath];
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [allStrings sizeWithFont:cellFont
constrainedToSize:constraintSize
lineBreakMode:NSLineBreakByWordWrapping];
return labelSize.height + 20;
}
This gets called when the table is not empty, and the size gets calculated
correctly, now the issue is that if there is data in the Table, all the
cells get their height changed:
http://img716.imageshack.us/img716/3025/yrgt.png
I have tried checking for this in the delegate method
if([tableview cellForRowAtIndexPath:IndexPath] == nil)
return 20;
But this returns an error, i have also tried wrapping it in != nil, but
that has no effect, so how do i leave the empty rows at the default height
?
i am setting the heightForRowAtIndexPath like this:
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
/* Contains the string */
NSString *allStrings = [self convertMainData:indexPath];
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [allStrings sizeWithFont:cellFont
constrainedToSize:constraintSize
lineBreakMode:NSLineBreakByWordWrapping];
return labelSize.height + 20;
}
This gets called when the table is not empty, and the size gets calculated
correctly, now the issue is that if there is data in the Table, all the
cells get their height changed:
http://img716.imageshack.us/img716/3025/yrgt.png
I have tried checking for this in the delegate method
if([tableview cellForRowAtIndexPath:IndexPath] == nil)
return 20;
But this returns an error, i have also tried wrapping it in != nil, but
that has no effect, so how do i leave the empty rows at the default height
?
imageview height in gridview
imageview height in gridview
I follow this guide to insert images in a gallery using grid view:
http://developer.android.com/guide/topics/ui/layout/gridview.html
I edited the imageadapter code as follow:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some
attributes
imageView = new ImageView(mContext);
imageView.setId(R.id.img_gallery);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private int[] mThumbIds = {R.drawable.itinerario1_1,
R.drawable.itinerario1_1, R.drawable.itinerario1_1,
R.drawable.itinerario1_1, R.drawable.itinerario1_1,
R.drawable.itinerario1_1, R.drawable.itinerario1_1,
R.drawable.itinerario1_1, R.drawable.itinerario1_1,
R.drawable.itinerario1_1};
}
and this is img_gallery xml
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/img_gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:adjustViewBounds="true"/>
and this is the main layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:fadingEdge="none">
<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:stretchMode="columnWidth"
android:numColumns="2"
android:gravity="center"
android:padding="5dp"
/>
the problem is that when i tun the app, imageview has a lot of top and
bottom padding. I also tried to set it to 0 in the xml but it doesn't
change.
I follow this guide to insert images in a gallery using grid view:
http://developer.android.com/guide/topics/ui/layout/gridview.html
I edited the imageadapter code as follow:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some
attributes
imageView = new ImageView(mContext);
imageView.setId(R.id.img_gallery);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private int[] mThumbIds = {R.drawable.itinerario1_1,
R.drawable.itinerario1_1, R.drawable.itinerario1_1,
R.drawable.itinerario1_1, R.drawable.itinerario1_1,
R.drawable.itinerario1_1, R.drawable.itinerario1_1,
R.drawable.itinerario1_1, R.drawable.itinerario1_1,
R.drawable.itinerario1_1};
}
and this is img_gallery xml
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/img_gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:adjustViewBounds="true"/>
and this is the main layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:fadingEdge="none">
<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:stretchMode="columnWidth"
android:numColumns="2"
android:gravity="center"
android:padding="5dp"
/>
the problem is that when i tun the app, imageview has a lot of top and
bottom padding. I also tried to set it to 0 in the xml but it doesn't
change.
Subscribe to:
Posts (Atom)